从以下表单获取帖子数据后,页面应重定向到“关联:学习”,如操作中所示。但是,它只停留在单选按钮页面上。我怀疑我是在犯一个初学者的错误,但在重新阅读教程之后,我不确定是怎么回事。
的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>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
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)),
)
views.py
def index(request):
images = Image.objects.all()
datasets = []
for i in images:
if i.rank() >= 3:
datasets.append(i)
return render(request, 'associate/index.html', {'datasets':datasets})
原始HTML应重定向到此页面。
learn.html
THIS IS THE LEARN PAGE
答案 0 :(得分:2)
你能直接去关联:学习吗?
在你的第一个urls.py
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
网址将始终与“associate.views.index”匹配,因为它出现在“associate.views.learn”之前,并且它们都具有相同的网址。
您应该将其更改为:
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^learn_or_something$', "associate.views.learn", name='learn'),
)
希望这有帮助。
答案 1 :(得分:1)
您的关联“索引”和“学习”视图都具有相同的网址。你需要有一些方法来区分它们,否则URL将始终由第一个提供,即索引。