NoReverseMatch在/反转为'dashboard'时找不到参数。尝试了1个模式:['(?P <用户名> [^ /] +)\\ / $']

时间:2018-11-14 10:12:16

标签: django django-forms django-authentication

我收到此错误

profile page

用户单击登录按钮后,其经过身份验证并移至dashboard.html页面后,预期该URL应重定向到“ domain / username.html”。

login1.html

NoReverseMatch at /
Reverse for 'dashboard' with no arguments not found. 1 pattern(s) tried: ['(?P<username>[^/]+)\\/$'].

观看次数

{% load custom_filters %}
<a href="" class="btn-back-sign-up" id="dismiss" id="sign-up">
            <i class="fa fa-chevron-left"> &nbsp;SIGNUP</i>
        </a><br><br>
        <p class="text-join">Join our community that have more than 10,000<br> subscribers and learn new things everyday.</p>
        <div class='back' id='login_form'>
        <form id='login-form' method='post' {% url 'accounts:dashboard' %} ><br>
        {% csrf_token %}
            {% for field in form %}
                {% if field.errors %}
                     {% if form.errors %}
                    <p>Your username and password didn't match. Please try again.</p>
                    {% endif %}
                {% endif %}
                <div class="form-group">
                    {{ field.label_tag }}
                    {{ field|add_class:'form-control'}}
                </div>
            {% endfor %}
            <div class="form-group">
              <input type="text" class="form-control input-upper" id="username" placeholder="Username" name="username" required id="id_username"><br>
              <input type="password" class="form-control input-upper" id="password" placeholder="Password" name="password1" required id="id_password"><br>
              <button type="submit" value='Login' id="top-login-btn" class="btn btn-primary btn-block btn-signup-form-1">LOGIN</button>
              <p class="text-center">or</p>
              <button type="button" class="btn btn-primary btn-block btn-sign-linkedin-1">Login up with LinkedIn</button>
              <p class="text-already">Don't have an account?
               <a href="" data-toggle="modal" class="btn-back-sign-up"  data-target="#modal-signup" id="dismiss" id="sign-up">Sign Up</a>
              </p>

            </div>
        </form>
        </div>
        <p class="text-already"><a href="{% url 'password_reset' %}">Forgot Password?</a></p><br>

urls.py

@login_required
def dashboard(request, username):
    """Dashboard page for user"""

    if request.user.username != username:
        return redirect(
            'accounts:dashboard',
            args=(request.user.username,)
        )
    return render(request, 'accounts/dashboard.html', {
        'notifications': request.user.notifications.order_by('-date_added'),
        'sub_token': url_crypt.encode_token((request.user.subscription.plan,))
    })

1 个答案:

答案 0 :(得分:0)

来自网址:

path(r'<str:username>/', views.dashboard, name='dashboard'),

意味着,您需要在模板中使用用户名参数调用仪表板URL。您可以通过以下网址进行修复:

path(r'', views.dashboard, name='dashboard'),

并保持模板不变。