【问题标题】:yet another django url redirection issue另一个 django url 重定向问题
【发布时间】:2014-04-03 12:17:39
【问题描述】:

下面的视图应该加载一个页面,'learn.html',但它只是出于某种原因返回到索引。

def learn(request):

    try:

    ...  # everything goes smoothly here, the else clause is evaluated.

    else:
        return render(request, 'associate/learn.html', {'dataset':model, 'ordered_groups':ordered_groups})

这是我的网址:

图片\urls.py:

urlpatterns = patterns('',
    url(r'^images/', include('images_app.urls', namespace="images_app")),
    url(r'^associate/', include('associate.urls', namespace="associate")),
    url(r'^admin/', include(admin.site.urls)),
)

associate\urls.py 编辑:我不小心写了 images\urls.py - 现在更正了。

urlpatterns = patterns('',
    url(r'^learn/', "associate.views.learn", name='learn'),
    url(r'^$', "associate.views.index", name='index'),
)

这是加载正常的索引页面,用户应该通过单击学习按钮从该页面重定向到 learn.html。但是,索引页面只是重新加载。

关联\index.html

Choose a dataset 

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'associate:learn' %}" method="post">
{% csrf_token %}
{% for dataset in datasets %}
    <input type="radio" name="dataset" id="dataset{{ forloop.counter }}" value="{{ dataset.id }}" />
    <label for="dataset{{ forloop.counter }}">{{ dataset }}</label><br />
{% endfor %}
<input type="submit" value="learn" />
</form>

associate\learn.html

THIS IS THE LEARN PAGE

感谢您的帮助。

【问题讨论】:

    标签: python html django url redirect


    【解决方案1】:

    对于关联应用的 urls.py 试试这个

    urlpatterns = patterns('associate',
      url(r'^learn/', "associate.views.learn", name='learn'),
      url(r'^$', "associate.views.index", name='index'),
    )
    

    【讨论】:

    • 我添加了 'associate' 并收到错误“ImportError at /associate/learn/ No module named associate”我还需要添加什么吗?
    • associate 应该在 settings.py 的 INSTALLED_APPS 中
    • +Özgür Eroğlu 我的已安装应用程序部分确实有关联。不知道还有什么可能。
    • 好的,请尝试如下。 - 将 url(r'^associate/', include('associate.urls', namespace="associate")) 替换为 url(r'^associate/', include('associate.urls')) - 替换 url(r '^learn/', "associate.views.learn", name='learn') 和 url(r'^learn/', associate.views.learn)
    • +Özgür Eroğlu 刚刚尝试了您的建议,我得到了同样的错误:( 不过感谢您的帮助!。
    猜你喜欢
    • 2016-01-01
    • 2012-10-07
    • 2018-02-14
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多