Reputation: 427
I am trying to view a user profile for my website with the Detail CBV. Below is the code for views.py, urls.py, models.py and profile.html.
A user exists with the username "brian_weber", but for some reason when I navigate to this link: http://0.0.0.0:8000/accounts/profile/brian_weber the page is not found. My app is called "accounts".
If someone could point me in the right direction to get the view to show up with the url, that would be greatly appreciated! I have searched around Stack Overflow, but nothing has worked that I tried.
Thanks in advance,
Brian
views.py
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.contrib.auth import login, logout, authenticate
from django.http import HttpResponseRedirect
from django.contrib.auth.forms import AuthenticationForm
from django.core.urlresolvers import reverse, reverse_lazy
from django.views import generic
from braces.views import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from . import forms
from . import models
class LoginView(generic.FormView):
form_class = AuthenticationForm
success_url = reverse_lazy('home')
template_name = "accounts/login.html"
def get_form(self, form_class=None):
if form_class is None:
form_class = self.get_form_class()
return form_class(self.request, **self.get_form_kwargs())
def form_valid(self, form):
login(self.request, form.get_user())
return super().form_valid(form)
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse('home'))
class SignUp(SuccessMessageMixin, generic.CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("login")
template_name = "accounts/signup.html"
success_message = "Your profile has been successfully created. Please log into your account."
class UserProfile(LoginRequiredMixin, generic.DetailView):
model = models.UserProfile
template_name = "profile.html"
class UserProfileUpdate(LoginRequiredMixin, generic.UpdateView):
model = models.UserProfile
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^logout/$', views.logout_view, name="logout"),
url(r'^signup/$', views.SignUp.as_view(), name="signup"),
url(r'^profile/(?P<username>[a-zA-Z0-9]+)$',
views.UserProfile.as_view(),
name="profile"),
url(r'^profile/update/(?P<username>[a-zA-Z0-9]+)$',
views.UserProfileUpdate.as_view(),
name="update_profile"),
]
models.py
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
company = models.CharField(max_length=40, null=True)
position = models.CharField(max_length=40, null=True)
bio = models.CharField(max_length=140, blank=True, default="")
avatar = models.ImageField(blank=True, null=True, upload_to="avatars",
height_field=None, width_field=None)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
Upvotes: 0
Views: 809
Reputation: 4251
Your mistake is very small, the regular expression provided in url for profile, (?P<username>[a-zA-Z0-9]+)
does not match brian_weber
because of the _
.
You can simply update the regex to match _
too.
username_regex = r'[a-zA-Z0-9_]+'
urlpatterns = [
url(r'^logout/$', views.logout_view, name="logout"),
url(r'^signup/$', views.SignUp.as_view(), name="signup"),
url(r'^profile/(?P<username>{username})$'.format(username=username_regex),
views.UserProfile.as_view(),
name="profile"),
url(r'^profile/update/(?P<username>{username})$'.format(username=username_regex),
views.UserProfileUpdate.as_view(),
name="update_profile"),
]
Upvotes: 2