Trying to view User Profile in Django, but Page Not Found when searching URLs
<p>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.</p>

<p>A user exists with the username "brian_weber", but for some reason when I  navigate to this link: <a href="http://0.0.0.0:8000/accounts/profile/brian_weber" rel="nofollow">http://0.0.0.0:8000/accounts/profile/brian_weber</a> the page is not found. My app is called "accounts".</p>

<p>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.</p>

<p>Thanks in advance,</p>

<p>Brian</p>

<p><strong>views.py</strong>   </p>

<pre><code>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
</code></pre>

<p><strong>urls.py</strong></p>

<pre><code>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&lt;username&gt;[a-zA-Z0-9]+)$', 
        views.UserProfile.as_view(), 
        name="profile"),
    url(r'^profile/update/(?P&lt;username&gt;[a-zA-Z0-9]+)$', 
        views.UserProfileUpdate.as_view(), 
        name="update_profile"),
]
</code></pre>

<p><strong>models.py</strong></p>

<pre><code>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)
</code></pre>

<p>Your mistake is very small, the regular expression provided in url for profile, <code>(?P&lt;username&gt;[a-zA-Z0-9]+)</code> does not match <code>brian_weber</code> because of the <code>_</code>.</p>

<p>You can simply update the regex to match <code>_</code> too.</p>

<pre><code>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&lt;username&gt;{username})$'.format(username=username_regex), 
        views.UserProfile.as_view(), 
        name="profile"),
    url(r'^profile/update/(?P&lt;username&gt;{username})$'.format(username=username_regex),
        views.UserProfileUpdate.as_view(), 
        name="update_profile"),
]
</code></pre>

python django django-views django-class-based-views user-profile