0

这是我在登录页面中使用的模板,但问题是字段未显示。我想在页面上使用 mdbootstrap。我在许多网站上搜索过它,但没有找到解决方案,每个人都在使用使用表单的相同之处是我的代码中缺少的东西?

<form action="{% url 'log_in' %}" method="POST">

              {% csrf_token %}

    <div class="md-form">

        <i class="fa fa-envelope prefix"></i>
        {{ form.username }}
        {{ form.username.label_tag }}

     </div>

    <div style="padding:5px"></div>

    <div class="md-form"  >

        <i class="fa fa-lock prefix"></i>
        {{ form.password.label_tag }}
        {{ form.password }}

    </div>
            {% if requset.GET.next %}
            <input type="hidden" name="next" value="{{ request.GET.next }}">
            {% endif %}

    <button type='submit' class="btn info-color ">log in</button>

 </form>

和forms.py

from django.contrib.auth.forms import AuthenticationForm
from django import forms


class LoginForm(AuthenticationForm):
    username = forms.CharField(label="Username", max_length=30,
                               widget=forms.TextInput(attrs={'class': 'form-control', 'name': 'username'}))
    password = forms.CharField(label="Password", max_length=30,
                                widget=forms.PasswordInput(attrs={'class': 'form-control', 'name': 'password'}))

我的视图功能是

def log_in(request):
    if request.user.is_authenticated:
        return render(request,'registration/userhome.html')
    elif request.POST:
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user.is_active:
            login(request, user)
            return render(request,"registration/userhome.html")
    else :
        return HttpResponse("Invalid login details supplied.")        

查看 login_view()

def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = LoginForm()
    return render(request,"registration/login.html",{'form':form})

我什至没有渲染登录页面,但它们仍在工作,并且其中缺少表单字段的一部分

我的 urls.py 文件

from django.urls import path
from . import views
from django.contrib.auth.views import LoginView


urlpatterns =[

    path("",views.index,name='index'),
    path("userhome/",views.userhome,name='userhome'),
    path("quiz/", views.quiz, name='quiz'),
    path("signup/", views.sign_up,name='sign_up'),
    path("login/", views.login_view,  name='login'),
    path("login/", views.log_in,  name='log_in'),
    path("index", views.log_out,name='log_out'),
    path("rules/",views.rules,name='rules'),

]
4

1 回答 1

0

你必须有这样的观点:

def myView(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = LoginForm()
    return render(request, 'myTemplate.html', {'form': form})
于 2018-08-01T04:51:11.150 回答