我按照本页面中的说明操作 ' homepage login form Django'
为了将登录表单直接放到网站的主页上, 而不是有一个单独的登录和注册页面,
但上面的链接并未显示views.py和base.html
因此我不知道如何使表格可见。
如果您访问实际网站:http://mtode.com
它缺少ID和密码表单,而且当我尝试在其他表单上键入时,它会抛出一个错误,这是一个发布内容的帖子。
这是views.py
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
redirect_url = '/'
else:
messages.error(request, "Not Successfully Created")
else:
messages.success(request, "Successfully Created")
@login_required
def post_list(request):
form = PostForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
print (form.cleaned_data.get("title"))
instance.save()
# message success
messages.success(request, "Successfully Created")
return HttpResponseRedirect(instance.get_absolute_url())
#else:
#messages.error(request, "Not Successfully Created")
queryset = Post.objects.all()#.order_by("-timestamp")
context = {
"object_list": queryset,
"title": "List",
"form": form,
}
return render(request, "post_list.html", context)
和
urls.py
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout')
(虽然我不知道为什么在将以下代码放入settings.py进行重定向之后我必须将这些代码用于什么用途)
settings.py
LOGIN_URL = reverse_lazy('login')
LOGIN_REDIRECT_URL = reverse_lazy('home')
最后是post_list.html
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="">Lost password?</a></p>
<div class='two columns right mgr'>
<h1>Form</h1>
<form method='POST' action=''>{% csrf_token %}
{{ form.as_p }}
<input class="button-primary" type='submit' value='Create Post' />
</form>
</div>
<div class='four columns left'>
<h1>{{ title }}</h1>
{% for obj in object_list %}
<div class="row">
<div>
<a href='{{ obj.get_absolute_url }}'>
<div class="thumbnail">
<!--<img src="..." alt="...">!-->
<div class="caption">
<h3>{{ obj.title }}<small> {{ obj.timestamp|timesince }} ago</small></h3>
<p>{{ obj.content|linebreaks|truncatechars:120 }}</p>
<!-- <p><a href='{{ obj.get_absolute_url }}' class="btn btn-primary" role="button">View</a> </p>-->
</div>
</div></a>
</div>
<hr />
</div>
{% endfor %}
</div>
{% endblock content %}
答案 0 :(得分:0)
您的第一张表单未正确设置。一种解决方案可以是做像htis这样的事情:
<form action="/login/?next=/home/" method="post">
{% csrf_token %}
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<button type="submit">Sign in</button>
</form>
行动中的网址必须转到您的登录信息,下一个链接将是登录后的返回位置。