Django登录表单输入字段未显示 [英] Django Login Form Input Fields not showing up

查看:205
本文介绍了Django登录表单输入字段未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django Web项目上为一个类工作。

working on a Django Web project for a class.

为登录页面创建了一个自己的模板,看起来像这样

Created an own template for the login page, that looks like this

{% extends 'thirdauth/base.html' %}
{% block content %}
<h2>Login</h2>
<div class="login-form">
  <form method="post" action="{% url 'thirdauth:login' %}">
      {{form.as_p}}
      {% csrf_token %}
      <input type="hidden" name="next" value="{{ next }}" />
      <p><input type="submit" value="Login"></p>
  </form>
</div>
<br>
<p><strong>-- OR --</strong></p>
<a href="{% url 'thirdauth:social:begin' 'github' %}">Login with GitHub</a><br>
{% endblock %}

我面对的唯一问题是输入字段最初加载网站时,没有显示用户名和密码+标签。当我单击登录按钮时,将显示字段,我可以输入所有内容,并且一切正常...

The only problem that I face is that the input fields for user and password + labels are not showing up when the sites initially loads. When I click on the Login button, the fields appear and I can enter everything and it works fine...

这里是urls.py

Here is the urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views as auth_views
from . import views

urlpatterns = [
            url(r'^login/$', auth_views.login, name='login'),
            url(r'^logout/$', auth_views.logout, name='logout'),
            url(r'^auth/', include('social_django.urls', namespace='social')),
            url(r'^admin/', admin.site.urls),
            url(r'^accounts/login/$', views.authentication, name='authentication'),
]

我很感谢您的帮助,我认为这只是我的一个小愚蠢错误,但我找不到

I'm thankful for help, I think it is just a small stupid mistake from me, but I'm not able to find it, most help on the internet focuses on self defined forms.

EDIT
views.py:

EDIT views.py:

from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.shortcuts import render, get_object_or_404

def authentication(request):
    return render(request, 'registration/login.html')

编辑2
second views.py:

EDIT 2 second views.py:

from django.utils import timezone
from .models import *
from django.shortcuts import render, get_object_or_404
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
import wikipediaapi

def home(request):
  return render(request, 'worldmap/home.html',
             {'worldmap': home})

def denverText (request):
    wiki_wiki = wikipediaapi.Wikipedia('en')
    page_py = wiki_wiki.page('Denver')
    return page_py.summary[0:60];

@login_required
def plannerGetStarted (request):
    return render(request, 'worldmap/planner.html')

已解决:
将此代码添加到我的views.py中:

SOLVED: added this code to my views.py:

def authentication(request):
if request.method == 'POST':
    form = LoginForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        user = authenticate(username=cd['username'],
                    password=cd['password'])
        if user is not None:
            if user.is_active:
                login(request, user)
                #return HttpResponse('Authenticated ' \
                 #       'successfully')
                return HttpResponseRedirect('/home/')
        else:
            return HttpResponse('Disabled account')

    else:
        return HttpResponse('Invalid login')
else:
    form = LoginForm()

return render(request, 'registration/login.html', {'form': form})


推荐答案

这就是我认为这里正在发生的事情。如果我说对了,那么您首先要导航到/ accounts / login。

So here is what i think is happening here. If i'm right in saying that you are navigating to /accounts/login first.

当您进入/ accounts / login时,django使用身份验证视图,该视图仅显示html而不创建表单。这就是为什么您最初看不到字段的原因。

When you go to /accounts/login django uses your authentication view which just renders the html without creating a form. This is why you don't see the fields initially.

该表单已设置为提交到/ login /,因为操作设置为 {%url'thirdauth:login '%}。 / login /使用默认的django登录视图。因此,一旦您对不带任何字段的表单进行汇总,就会在默认视图中导致错误,该错误会将您重定向回/ login /。这就是为什么在单击提交后看到带有此字段为必填错误的字段的原因。

The form is setup to submit to /login/ because action is set to "{% url 'thirdauth:login' %}". /login/ is using the default django login view. So once you sumbit the form with no fields this causes an error in the defualt view which redirects you back to /login/. This is why you then see the fields with the "this field is required" errors after clicking submit.

希望对此进行解释的

这篇关于Django登录表单输入字段未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆