在Django中传递消息时重定向 [英] Redirect while passing message in django
问题描述
在检查用户是否存在user_settings
后,我尝试运行重定向(如果不存在-将用户带到表单中进行输入和保存).
I'm trying to run a redirect after I check to see if the user_settings
exist for a user (if they don't exist - the user is taken to the form to input and save them).
我想将用户重定向到适当的表单,并向他们显示必须保存设置"的消息,以便他们知道为什么要重定向他们.
I want to redirect the user to the appropriate form and give them the message that they have to 'save their settings', so they know why they are being redirected.
函数如下:
def trip_email(request):
try:
user_settings = Settings.objects.get(user_id=request.user.id)
except Exception as e:
messages.error(request, 'Please save your settings before you print mileage!')
return redirect('user_settings')
此功能检查用户设置并适当地重定向我-但该消息永远不会出现在模板顶部.
This function checks user settings and redirects me appropriately - but the message never appears at the top of the template.
您可能首先想到:您的Django中是否正确设置了消息?"
我还有其他功能,可以使用不是 重定向的消息,消息在模板中按预期显示,没有问题.邮件已正确集成到我的模板中,并且可以正常工作.
I have other functions where I use messages that are not redirects, the messages display as expected in the template there without issue. Messages are integrated into my template appropriately and work.
仅当我使用redirect
时,我才看不到正在发送的消息.
Only when I use redirect
do I not see the messages I am sending.
如果我像下面那样使用render
,我会看到消息(但是,URL不会改变-我希望发生这种情况).
If I use render
like the following, I see the message (but of course, the URL doesn't change - which I would like to happen).
def trip_email(request):
try:
user_settings = Settings.objects.get(user_id=request.user.id)
except Exception as e:
messages.error(request, 'Please save your settings before you print mileage!')
form = UserSettingsForm(request.POST or None)
return render(request, 'user/settings.html', {'form': form})
我还有其他一些地方需要使用redirect
,因为这样做在功能上是合理的-但我也想将消息传递给那些重定向.
I have a few other spots where I need to use a redirect
because it functionally makes sense to do so - but I also want to pass messages to those redirects.
user_settings
函数如下所示:
def user_settings(request):
try:
user_settings = Settings.objects.get(user_id=request.user.id)
form = UserSettingsForm(request.POST or None, instance=user_settings)
except Settings.DoesNotExist:
form = UserSettingsForm(request.POST or None)
if request.method == 'POST':
settings = form.save(commit=False)
settings.user = request.user
settings.save()
messages.warning(request, 'Your settings have been saved!')
return render(request, 'user/settings.html', {'form': form})
我在文档中找不到任何内容,说明您无法使用重定向发送邮件...但是我不知道如何显示它们.
I can't find anything in the documentation saying that you can't send messages with redirects... but I can't figure out how to get them to show.
这就是我在模板中呈现消息的方式:
This is how I render the messages in the template:
{% for message in messages %}
<div class="alert {{ message.tags }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{{ message }}
</div>
{% endfor %}
我不确定这是否重要-但似乎它几乎从某个地方两次调用'GET'.
I'm not sure if it matters - but it looks like it's almost calling 'GET' twice from somewhere.
这两个URL的URL部分如下所示:
the URL section looks like this for these two URLS:
# ex: /trips/email
url(r'^trips/email/$', views.trip_email, name='trip_email'),
# ex: /user/settings
url(r'^user/settings/$', views.user_settings, name='user_settings'),
推荐答案
不,我认为最好的方法是使用您的会话.
Nope I think the best way is to use your sessions.
处理您的表格
def handle_form(request):
...
request.session['form_message'] = "success or fail message here"
redirect('/destination/', {})
def page_to_render_errors():
...
message = None
if( 'form_message' in request.session ):
message = request.session['form_message']
del request.session['form_message']
messages.success = (request, message ) #<< if you choose to pass it through django's messaging
render(request,'template_name.html', {'message_disp':message })
如果你们都有更好的方法打我. 编码愉快!
If y'all got a better way hit me up. Happy coding!
这篇关于在Django中传递消息时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!