0

ユーザーメニューに 1 つの問題があります。そのため、認証されたユーザーは自分のプロフィールページとログアウト(リンク)をメニューで見ることができます。(ログイン時に)インデックスページで機能します:index、page1、profile、logoutですが、たとえばpage1に移動すると、メニューに表示されます:index、page1、login、profileとlogoutではありません。修正方法は?

URL で:

url(r'^accounts/login/$', 'django.contrib.auth.views.login' ),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login' ),
url(r'^accounts/profile/$', 'my_app.views.profile' ),

ビューで:

def profile(request):
    if  not request.user.is_authenticated():
        return HttpResponseRedirect("/accounts/login/")        
    else:
        user = request.user.is_authenticated()
        return  render_to_response('profile.html',locals()) 

index.html の一部:

{% if user.is_authenticated or request.user.is_authenticated %}
    <li><a href="/accounts/profile/">Profile</a></li>
    <li><a href="/accounts/logout/">logout</a></li>
{% else %}
    <li><a href="/accounts/login/">login</a></li>
{% endif %}

login.html:

{% extends "index.html" %}
{% load url from future %}

{% block application %}

{% if form.errors %}
<p>Try one more time</p>
{% endif %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% 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>

{% endblock %}

profile.html:

{% extends "index.html" %}
{% block application %}
{% if request.user.is_authenticated %}
    <p>Welcome, {{ request.user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}
{% endblock %}

page1.html:

{% extends "index.html" %}
{% block application %}
some for loops here
{% endblock %}

page1 のビュー:

def car(request):
    all_cars = Car.objects.all().filter(active=1).values('id', 'name')
    return render_to_response('page1.html', {'all_cars': all_cars})
4

2 に答える 2

0

userテンプレートをレンダリングするときに手動で渡す必要はありません。これを試してみてください。

view.py

from django.contrib.auth.decorators import login_required
from django.shortcuts import render

@login_required
def profile(request):
    return render(request, 'profile.html')

settings.py

LOGIN_URL = '/accounts/login/'

index.html:_

{% if user.is_authenticated %}
    <li><a href="/accounts/profile/">Profile</a></li>
    <li><a href="/accounts/logout/">logout</a></li>
{% else %}
    <li><a href="/accounts/login/">login</a></li>
{% endif %}

profile.html

{% extends "index.html" %}
{% block application %}
{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}
{% endblock %}

page1ビュー:

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
# If you want only authenticated users to access this page
@login_required
def car(request):
    all_cars = Car.objects.all().filter(active=1).values('id', 'name')
    return render(request, 'page1.html', {'all_cars': all_cars})

PS:URLをハードコーディングする代わりに、名前付きURLを使用し、URL名を使用することを強くお勧めします。

于 2012-10-25T11:33:15.690 に答える
0

常に追加context_instance:

render_to_response('profile.html',locals(), context_instance=RequestContext(request))
于 2012-10-25T10:57:48.090 に答える