0

我正在使用 django 注册,我想在登录后被重定向到另一个页面。我在settings.py中使用过:

LOGIN_REDIRECT_URL = 'http://127.0.0.1:8000/home/'

这就是我想要重定向到的页面。

我成功重定向。但是主页必须显示我数据库中的一些元素,并且什么都没有。元素存在于数据库中(我检查过)。

请问有什么想法吗?泰

我的第一个 url conf(django 的 url conf)=> urls.py

urlpatterns = patterns('',
    url(r'^accounts/', include('dashboard.registration.backends.simple.urls')),
    url(r'^home/$', direct_to_template,{ 'template': 'index.html' }, 'index'),
url(r'^home/', include(dashboard.home.urls)),
url(r'^admin/', include(admin.site.urls)),
)

我的第二个 url conf (dashboard.home.urls)

urlpatterns = patterns('dashboard.home.views',
    url(r'^$',index,name='index'),
)

我在 dashboard.home.views 中的索引请求

def index(request):
    print 'index request'
    project_list = Project.objects.all().order_by('project_name')
    return render_to_response('index.html',{'project_list': project_list})

我的模板 index.html

<body>
{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>

    <h3>Home</h3>
    <ul>
    <li><a href="Add_Project/">Add a Project</a></li>

    </ul>

    <h3>Project list</h3>
    {% if project_list %}
        <ul>
        {% for project in project_list %}
            <li> <a href={{project.project_name}}>{{ project.project_name }}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>There are no Projects</p>
    {% endif %} 

{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}
</body> 

我的设置.py

LOGIN_REDIRECT_URL = 'http://127.0.0.1:8000/home/'
4

1 回答 1

2
url(r'^home/$', direct_to_template,{ 'template': 'index.html' }, 'index'),
url(r'^home/', include(dashboard.home.urls)),

Django 将在第一次匹配时停止,因此如果 url 为/home/. 只有第一个模式会匹配,并index.html会显示出来。

你也应该通过RequestContext,最简单的方法是使用render快捷方式。

from django.shortcuts import render

def index(request):
    project_list = Project.objects.all().order_by('project_name')
    return render(request,'index.html',{'project_list': project_list})

In addition make sure you have not removed "django.contrib.auth.context_processors.auth" from TEMPLATE_CONTEXT_PROCESSORS (its there by default, unless you removed it).

EDIT:

I tried, I have the project list because I print it on console. But I have not the project list on my page.

Your project list will only print if you have a valid user; if you are logged in as guest, then your project list won't print anything.

Try this as your template:

<body>
{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}

    <h3>Home</h3>
    <ul>
    <li><a href="Add_Project/">Add a Project</a></li>

    </ul>

    <h3>Project list</h3>
    {% if project_list %}
        <ul>
        {% for project in project_list %}
            <li> <a href={{project.project_name}}>{{ project.project_name }}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>There are no Projects</p>
    {% endif %} 

</body> 

And adjust your index method:

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

@login_required
def index(request):
    project_list = Project.objects.all().order_by('project_name')
    return render(request,'index.html',{'project_list': project_list})
于 2012-08-24T14:23:33.207 回答