当我尝试登录用户时出现登录错误。姜戈

login error when I try to login the user. Django

大家晚上好,

我正在建立我的第一个网站,当我尝试登录用户时遇到以下两个问题:

  1. 我边写边看密码

  2. 当我点击提交时,我得到一个 404,因为它把我发送到错误的 URL:请求 URL:http://127.0.0.1:8000/login_user/simple_investing/create_company.htm/

这是我的代码:

Forms.py:

class UserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')


class UserLogin(ModelForm):
    class Meta:
        model = User
        fields= ('username', 'password')

Views.py:

def login_page(request):
    messages.success(request, 'Account created successfully')
    form = UserLogin
    if request.method == 'POST':       
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(request, username=username, password=password)             
        if user is not None:
            login(request, user)
            return HttpResponseRedirect('simple_investing/create_company.htm/')
        
    context = {'form' : form}
    return render(request, 'simple_investing/login.htm', context)

HTML:

{% for message in messages %}
    <p>{{ message }}</p>
{% endfor %}
    <p>Log in and start using our tools!</p>
<form action= "" method='post'>
   {% csrf_token %}

{% for field in form %}
    <p>{{field.label}}{{field}}</p>       
{% endfor %}

    <button type="submit" class="btn btn-success">Login User</button> 
 </form>

有什么提示吗?

正如 Lars 所说,您可以使用 Django 的内置登录功能: Django auth system

但是,在那之后,您仍然可以选择在使用

登录后重定向到哪里
LOGIN_REDIRECT_URL = '/profiles/home'

在你身上settings.py