[英]Django redirect back to previous page on form post
我正在嘗試將表單發布重定向到它發布的頁面。 因此,我的應用程序有一個詳細頁面,該頁面顯示有關地點/ arena / 123的信息,並且在同一頁面上,用戶可以輸入一種形式來輸入有關他們正在查看的當前地點的評論或評分。 這些是我的視圖功能。
class DetailView(LoginRequiredMixin, generic.DetailView):
model = Arena
template_name = 'arenas/detail.html'
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
if 'form' not in context:
print 'no form in context'
context['form'] = RatingForm
return context
def rate(request):
if request.method == 'POST':
form = RatingForm(request.POST)
if form.is_valid():
# post was valid. go back to page and display data
return DetailView.as_view()(request(), form=RatingForm)
else:
# post was unsuccessful. go back to the page and display errors
print form.errors
return DetailView.as_view()(request(), form=form)
和我的模板
{% extends "base.html" %}
{% block content %}
<h1>{{ arena.address }}</h1>
<h1>{{ arena.address_2 }}</h1>
<h1>{{ arena.phone }}</h1>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<form action="/arenas/rate/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
<div>
{{comments}}
</div>
{% endblock %}
所以我想做的是,當用戶在頁面上提交表單后,我想返回到我所在的同一詳細信息頁面,並且如果有用戶在其中張貼的數據,成功或重定向到同一頁面,但如果不成功,則會出現表格錯誤。 我的問題是現在不知道如何保留網址和表單錯誤以再次顯示詳細信息頁面。 我不認為我可以使用return DetailView.as_view()(context(), form=form)
因為我的DetailView沒有帖子,所以我得到405。我看到的每個示例都不涵蓋重定向回同一頁。 它們通常只顯示HttpResponseRedirect(/thanks)
或類似內容。
另請注意。 我不想發布到/ arena / 123,因為最終這將是更新有關該地點的信息的另一個功能,因此我不想將評論發布到該url上。
因此,我深入研究了一些Django示例,並偶然發現了SingleObjectMixin
。 這與FormView
結合似乎是我想要的。 Django文檔有,這是99.9%的什么我想要的例子在這里 。 只需確保閱讀更好的解決方案部分。
我對此改變了看法
class DetailView(LoginRequiredMixin, generic.DetailView):
model = Arena
template_name = 'arenas/detail.html'
def get_context_data(self, **kwargs):
print kwargs
context = super(DetailView, self).get_context_data(**kwargs)
context['form'] = RatingForm
return context
class RatingView(LoginRequiredMixin, detail.SingleObjectMixin, generic.FormView):
model = Arena
template_name = 'arenas/detail.html'
form_class = RatingForm
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super(RatingView, self).post(request, *args, **kwargs)
def get_success_url(self):
print "successfully posted"
return reverse('arenas:detail', kwargs={'pk': self.object.pk})
添加了使用/ rate發布表單的路線
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$',
views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/rate/$', views.RatingView.as_view(), name='rate')
)
並修改了我的模板以將對象的id
傳遞給表單發布操作中的路線
{% extends "base.html" %}
{% block content %}
<h1>{{ arena.address }}</h1>
<h1>{{ arena.address_2 }}</h1>
<h1>{{ arena.phone }}</h1>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<form action="/arenas/{{ arena.id }}/rate/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
<div>
{{comments}}
</div>
{% endblock %}
現在在發生錯誤時保留上下文,並且SingleObjectMixin / FormView組合使我停留在同一頁面上以顯示表單錯誤,並在成功發布get_success_url
其重定向到我想使用get_success_url
再次使用新信息加載頁面的詳細信息頁面那被張貼了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.