【问题标题】:Custom Django url for users用户的自定义 Django url
【发布时间】:2020-02-26 00:58:48
【问题描述】:

您好,我一直在尝试为我的用户创建客户网址,但它不起作用。我不断收到 NoReverseMatchError

这是我的 user_login views.py

def user_login(request):
    '''
    Using different method for getting username, tried this and didnt work either
    '''
    #if request.user.is_authenticated():
        #return HttpResponseRedirect('main:home')
    if request.method == 'POST':
        form = AuthenticationForm(request, data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:              
                login(request,user)
                messages.info(request, "Successfully signed in")
                return HttpResponseRedirect('home')
                #return HttpResponseRedirect(reverse('main:home', kwargs={'username': username})) 
            else:
                message = 'Sorry, the username or password you entered is not valid please try again.'
                return render(request, 'login.html', {'message':message})
        else:
            message = 'Sorry, the username or password you entered is not valid please try again.'
            return render(request, 'login.html', {'message':message})

这是我登录 login.html 的表单

<form method="POST" action="{%url 'main:home' user.username %}" class="form-signin">

这是我的“家”视图

def home(request, username):
    username = request.user.get_username()
    return render(request,'home.html')

这是我在 myapp/urls.py 的 url 路径

path('home/<username>', views.home, name='home'),

这是 LOGIN_REDIRECT_URL

LOGIN_REDIRECT_URL = '/main/home'

我正在尝试获取 mysite.com/home/myusername,但它给了我一个 NoReverseMarch 错误

错误:

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

我错过了什么吗?

【问题讨论】:

  • 您可以尝试渲染user.username 的值并检查它是否不是空字符串吗?

标签: python django django-views django-login


【解决方案1】:

对于,你不能使用这样的path(..)s,在这种情况下你需要写一个正则表达式,比如:

    <b>url</b>(<b>r</b>'<b>^</b>complete/<b>(?P</b>&lt;todo_id&gt;<b>[0-9]+)</b><b>$</b>', views.completeTodo, name='complete'),

如果您使用的是,您可能希望像您一样使用path(..)

我认为这可能与您设置正则表达式的方式有关。

对于 url,而不是这个:

url('complete/&lt;todo_id&gt;', views.completeTodo, name='complete'),

试试这个:

url(r'^complete/(?P<todo_id>\d+)$', views.completeTodo, name='complete'),

或者如果你想使用 [path]

path('complete/<int:todo_id>', views.completeTodo, name='complete'),

【讨论】:

    【解决方案2】:

    尝试使用 slug 作为用户名。因为当用户名在单词之间有空格时,URL 会分成两部分。 Slug 将用 - 替换空格。

    例如用户名为 John Mathew ,django 将搜索 home/john 而不是 home/john mathew 。如果您使用 slug 作为用户名,它将像这样发送用户名home/john-mathew....试试看

    【讨论】:

      【解决方案3】:

      问题在于 LOGIN_REDIRECT_URL 和您创建的 URL。

      根据

      LOGIN_REDIRECT_URL = '/main/home'
      

      登录后,用户被重定向到/main/home。 不幸的是,您的 URL 声明中不存在这样的路径。 相反,您在 urls.py 中的“主页”URL 有一个附加参数usernameLOGIN_REDIRECT_URL 没有找到该参数,因此会发生错误。

      从 settings.py 中删除 LOGIN_REDIRECT_URL,它应该可以按您的意愿工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 2021-11-10
        • 2013-09-27
        • 2016-08-13
        • 1970-01-01
        • 2013-02-16
        • 1970-01-01
        相关资源
        最近更新 更多