pythondjangourlvariables

Is there any specific way Django URL variables?


My Code is below, if I pass posts data via context, than i get "Reverse for 'user_profil' not found. 'user_profil' is not a valid view function or pattern name." without any "context", i get no error. I have read lots of the comments but no one points out this problem!

If someone can help me , I will be pleasure!

view function:

def user_profil(request,  username):

    posts = postList.objects.all().filter(author__username__icontains=username)

    context = {}
    context['posts'] = posts

    return render(request, 'profile.html', context=context)

Url_patterns:

urlpatterns = [
    path('', postlist_view.home, name='home'),
    path('signup', core_view.signup, name='signup'),
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
    path('article/upload/', article_view.upload, name='article-upload'),
    path('article/list/', article_view.list, name='article-list'),
    path('article/<int:pk>/', article_view.delete_article, name='delete_article'),
    path('post/<int:id>/detail/', postlist_view.post_detail, name='post_detail'),
    path('post/<int:id>/update/', postlist_view.post_update, name='post_update'),
    path('post/<int:id>/delete/', postlist_view.post_delete, name='post_delete'),
    path('post/create/', postlist_view.post_create, name='post_create'),
    path('profil/<usernaname>/',
         postlist_view.user_profil, name='page_of_user'),
    path('accounts/logout', core_view.signOut, name='logout'),



home.html

<form method="post">
 <a href="{% url 'page_of_user' username=post.author %}"><h3>{{post.author}}</h3></a></form>

profile.html

 <form method="post"><a href="{% url 'page_of_user' username=post.author %}"><h3>{{post.author}}</h3></a></form>`
   <a href="{% url 'user_profil'%}"><h3>{{post.author}}</h3></a>

Eror

Traceback (most recent call last):
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/alex/Documents/python_programming/vitor/filesystem/postlist/views.py", line 95, in user_profil
    return render(request, 'profile.html', context=context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/template/defaulttags.py", line 443, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/home/alex/Documents/python_programming/vitor/vit/lib/python3.7/site-packages/django/urls/resolvers.py", line 677, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'user_profil' not found. 'user_profil' is not a valid view function or pattern name.

Solution

  • This line of code seems to be the problem:

    <a href="{% url 'user_profil'%}"><h3>{{post.author}}</h3></a>
    

    From the urlpatterns you provided, there doesn't seem to be a url with the name 'user_profil'. Instead, you might want to use the same href as the other anchor tag as shown bellow:

    <a href="{% url 'page_of_user' username=post.author %}"><h3>{{post.author}}</h3></a>
    

    Edit: Thanks to Alasdair for pointing out the typo in this line:

    path('profil/<usernaname>/', postlist_view.user_profil, name='page_of_user'),
    

    You need to match the url parameter to the one used in the url tag in the HTML, so you should to change it to:

    path('profil/<username>/', postlist_view.user_profil, name='page_of_user'),