0

用户登录后,URL 未更改且未获取数据。

登录后 url 未更改(我预计更改为 127.0.0.1:8000/employer/home)

@login_required
def home(request):
    emp=Employer.objects.get(user=request.user)
    jb=Job.objects.filter(employer=emp).order_by("-postdate")
    return render(request,'employer/home.html',{'jobs':jb})#,{'employer':emp})

雇主/home.html

<table class="table table-hover table-bordered">
<tr><th>Job ID</th><th>Title</th><th>Posted on</th></tr>
{% for job in jobs %}
<tr>
<td><a href="{% url quorum.views.questions job.pk %}">{{ job.pk }}</a></td>
<td>{{ job.title }}</td>
<td>{{ job.postdate }}</td>
</tr>
{% endfor %}
</table>

登录.html

<form class="form-signin" action="" method="post">{% csrf_token %}
<h4 class="form-signin-heading">Sign in to my account...</h4>
{% if errors %}
<p class="text-error">Sorry, that's not a valid username or password</p>
{% endif %}

<input type="text" id="username" class="input-block-level" name="username" placeholder="UserID">
<input type="password" id="password" class="input-block-level" name="password" placeholder="Password">
<p style="text-align:right;"><a href="/accounts/password/reset/">Forgot</a> your password?</p>

<button class="btn btn-large btn-success" type="submit" value="login">Sign in</button>

</form>
4

1 回答 1

2

You need to tell it where to go after login using the next url parameter:

<form class="form-signin" action="{% url django.contrib.auth.views.login %}?next=employer/home/" method="post">

or set the LOGIN_REDIRECT_URL in settings:

The URL where requests are redirected after login when the contrib.auth.login view gets no next parameter.

于 2013-06-19T12:33:27.473 回答