0
votes

I have this error with templates, I don't understand what's the problem, pls help

NoReverseMatch at /login/ Reverse for '' not found. '' is not a valid view function or pattern name. Request Method: GET Request URL: http://localhost:5000/login/ Django Version: 3.1 Exception Type: NoReverseMatch Exception Value:
Reverse for '' not found. '' is not a valid view function or pattern name. Exception Location: /home/daniiar/.local/lib/python3.8/site-packages/django/urls/resolvers.py, line 685, in _reverse_with_prefix Python Executable: /usr/bin/python3 Python Version: 3.8.5 Python Path:
['/home/daniiar/land-gPage', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/daniiar/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages'] Server time: Sat, 16 Jan 2021 13:39:04 +0600 Error during template rendering In template /home/daniiar/land-gPage/auto/templates/base.html, error at line 14 Reverse for '' not found. '' is not a valid view function or pattern name.

FULL TRACEBACK

Traceback (most recent call last): File "/home/daniiar/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/daniiar/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 202, in _get_response response = response.render() File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/response.py", line 105, in render self.content = self.rendered_content File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content return template.render(context, self._request) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render return self.template.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 170, in render return self._render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 162, in _render return self.nodelist.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render bit = node.render_annotated(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render return compiled_parent._render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 162, in _render return self.nodelist.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render bit = node.render_annotated(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render result = block.nodelist.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render bit = node.render_annotated(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render return nodelist.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render bit = node.render_annotated(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated return self.render(context) File "/home/daniiar/.local/lib/python3.8/site-packages/django/template/defaulttags.py", line 446, in render url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app) File "/home/daniiar/.local/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "/home/daniiar/.local/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for '' not found. '' is not a valid view function or pattern name.

base.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <title>{% block title %}{% endblock %}</title>
  <link href="{% static 'css/base.css' %}" rel="stylesheet">
</head>
<body>
  <div id="header">
    <span class="logo">Bookmarks</span>
    {% if request.user.is_authenticated %}
      <ul class="menu">
        <li {% if section == 'dashboard' %}class="selected"{% endif %}>
          <a href="{% url 'dashboard' %}">My dashboard</a>
        </li>
        <li {% if section == 'images' %}class="selected"{% endif %}>
          <a href="#">Images</a>
        </li>
        <li {% if section == 'people' %}class="selected"{% endif %}>
          <a href="#">People</a>
        </li>
      </ul>
    {% endif %}

    <span class="user">
      {% if request.user.is_authenticated %}
        Hello {{ request.user }},
        <a href="{% url 'logout' %}">Logout</a>
      {% else %}
        <a href="{% url 'login' %}">Log-in</a>
      {% endif %}
    </span>
  </div>
  {% if messages %}
    <ul class="messages">
      {% for message in messages %}
        <li class="{{ message.tags }}">
          {{ message|safe }}
            <a href="#" class="close">x</a>
        </li>
      {% endfor %}
    </ul>
  {% endif %}
  <div id="content">
    {% block content %}
    {% endblock %}
  </div>
</body>
</html>

login.html

{% extends "base.html" %}

{% block title %}Log-in{% endblock %}

{% block content %}
  <h1>Log-in</h1>
  {% if form.errors %}
    <p>
      Your username and password didn't match.
      Please try again.
    </p>
  {% else %}
    <p>Please, use the following form to log-in. If you don't have an account <a href="{% url '' %}">register here</a></p>
  {% endif %}
  <div class="login-form">
    <form action="{% url 'login' %}" method="post">
      {{ form.as_p }}
      {% csrf_token %}
      <input type="hidden" name="next" value="{{ next }}" />
      <p><input type="submit" value="Log-in"></p>
    </form>
    <p><a href="{% url '' %}">Forgotten your  password?</a></p>
  </div>
<!--  <div class="social">-->
<!--    <ul>-->
<!--      <li class="facebook"><a href="{% url "social:begin" "facebook" %}">Sign in with Facebook</a></li>-->
<!--      <li class="twitter"><a href="{% url "social:begin" "twitter" %}">Login with Twitter</a></li>-->
<!--      <li class="google"><a href="{% url "social:begin" "google-oauth2" %}">Login with Google</a></li>-->
<!--    </ul>-->
<!--  </div>-->
{% endblock %}

views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import *
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import *
from .models import *
from django.views import View
from .forms import FeedbackForm


def formuser(request):
    return render(request, 'account/testregister.html')

def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(request, username=cd['username'],password=cd['password'])
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponse('Authenticated '\
                                        'successfully')
                else:
                    return HttpResponse('Disabled account')
            else:
                return HttpResponse('Invalid login')
    else:
        form = LoginForm()
    return render(request, 'auto/login.html', {'form': form})


@login_required
def dashboard(request):
    return render(request,
                  'auto/dashboard.html',
                  {'section': 'dashboard'})


def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if user_form.is_valid():
            new_user = user_form.save(commit=False)
            new_user.set_password(
                user_form.cleaned_data['password'])
            new_user.save()
            Profile.objects.create(user=new_user)
            return render(request,
                          'auto/register_done.html',
                          {'new_user': new_user})
    else:
        user_form = UserRegistrationForm()
    return render(request,
                  'auto/register.html',
                  {'user_form': user_form})


@login_required
def edit(request):
    if request.method == 'POST':
        user_form = UserEditForm(instance=request.user,data=request.POST)
        profile_form = ProfileEditForm(instance=request.user.profile, data=request.POST, files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Profile updated successfully')
        else:
            messages.error(request, 'Error updating your profile')
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)
    return render(request,
                  'auto/edit.html',
                  {'user_form': user_form,
                   'profile_form': profile_form})


@login_required
def editPartner(request):
    if request.method == 'POST':
        user_form = UserEditForm(instance=request.user,data=request.POST)
        partner_form = PartnerEditForm(instance=request.user.profile, data=request.POST, files=request.FILES)
        if user_form.is_valid() and partner_form.is_valid():
            user_form.save()
            partner_form.save()
            messages.success(request, 'Profile updated successfully')
        else:
            messages.error(request, 'Error updating your profile')
    else:
        user_form = UserEditForm(instance=request.user)
        partner_form = PartnerEditForm(instance=request.user.partner)
    return render(request,
                  'auto/edit.html',
                  {'user_form': user_form,
                   'partner_form': partner_form})




class FeedbackView(View):
    
    def post(self, request):
        
        if request.method == 'POST':
            form = FeedbackForm(request.POST)
        if form.is_valid():
            form.save()
            phoneNumber = form.cleaned_data['phoneNumber']
            name = form.cleaned_data['name']
            text = form.cleaned_data['text']
            subject = 'Новое сообщение'
            message = 'Новая заявка!' + '\r\n' + '\r\n' + 'Номер телефона: ' + phoneNUmber + '\r\n' + '\r\n' + 'ФИО:' + name + '\r\n' + 'Сообщение' + text
            bot.send_message(628980737, message)
        return redirect('home') 

urls.py


from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views


urlpatterns = [

    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('', views.dashboard, name='dashboard'),
    path('register/', views.register, name='register'),
    path('formregister/', views.formuser, name="formuser"),
    # change password urls
    # path('password_change/', auth_views.PasswordChangeView.as_view(), name='password_change'),
    # path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
    # reset password urls
    # path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
    # path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    # path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    # path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),


   
]

What's the matter?

1
Can you add the full traceback and the view that handles /login/ to your question? It's not obvious that it's an issue with your templateIain Shelvington
okk, one minuteDaniiar Biibosunov
You're passing an empty string to your register here link url tag in login.html. You should be passing a valid url path nameIain Shelvington
No, it doesn't help, I triedDaniiar Biibosunov
What doesn't help? The correct path name seems to be "register", have you tried <a href="{% url 'register' %}">register here</a>?Iain Shelvington

1 Answers

1
votes

The cause of this error is the following line in your login.html:

<p>Please, use the following form to log-in. If you don't have an account
<a href="{% url '' %}">register here</a></p>

By calling the url template tag with an empty argument, you will naturally get the error Reverse for '' not found. The solution is to specify the url name:

<p>Please, use the following form to log-in. If you don't have an account
<a href="{% url 'register' %}">register here</a></p>

Probably just an oversight on your part, happens to me all the time :-)