这里使用的是django1.5
需求: 有一个界面A,其中有一个form B, 前台提交B之后,后台保存数据之后,返回界面A,如果保存失败需要在A界面提示错误。
这里就需要后台的重定向,而且需要可以带着参数,也就是error message
这里收集了几种方法,简答说下需要那些包,怎么简单使用。
一、 使用HttpResponseRedirect
The first argument to the constructor is required – the path to redirect to. This can be a fully qualified URL (e.g.'http://www.yahoo.com/search/') or an absolute path with no domain (e.g. '/search/')。 参数既可以使用完整的url,也可以是绝对路径。
from django.http import HttpResponseRedirect
@login_required
def update_time(request):
#pass ... form处理
return HttpResponseRedirect('/commons/invoice_return/index/') #跳转到index界面
如果需要传参数,可以通过url参数
return HttpResponseRedirect('/commons/invoice_return/index/?message=error') #跳转到index界面
这样在index处理函数中就可以get到错误信息。
二、 redirect和reverse
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
#https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/
@login_required
def update_time(request):
#pass ... form处理
return redirect(reverse('commons.views.invoice_return_index', args=[])) #跳转到index界面
redirect 类似HttpResponseRedirect的用法,也可以使用 字符串的url格式 /..inidex/?a=add
reverse 可以直接用views函数来指定重定向的处理函数,args是url匹配的值。 详细请参见文档
三、 其他
其他的也可以直接在url中配置,但是不知道怎么传参数。
from django.views.generic.simple import redirect_to
在url中添加 (r'^one/$', redirect_to, {'url': '/another/'}),
我们甚至可以使用session的方法传值
request.session['error_message'] = 'test'
redirect('%s?error_message=test' % reverse('page_index'))
这些方式类似于location刷新,客户端重新指定url。
还没找到怎么在服务端跳转处理函数,直接返回response到客户端的方法。
2014-11-13 研究:
是不是之前的想法太死板,重定向,如果需要携带参数,那么能不能直接调用views中 url对应的方法来实现呢,默认指定一个参数。
例如view中有个方法baseinfo_account, 然后另一个url(对应view方法为blance_account)要重定向到这个baseinfo_account。
url中的配置:
urlpatterns = patterns('',
url(r'^baseinfo/', 'account.views.baseinfo_account'),
url(r'^blance/', 'account.views.blance_account'),
)
@login_required
def baseinfo_account(request, args=None):
#按照正常的url匹配这么写有点不合适,看起来不规范
if args:
print args
return render(request, 'accountuserinfo.html', {"user": user})
@login_required
def blance_account(request):
return baseinfo_account(request, {"name": "orangleliu"})
需要测试为:
1 直接访问 /baseinfo 是否正常 (测试ok)
2 访问 /blance 是否能正常的重定向到 /baseinfo 页面,并且获取到参数(测试ok,页面为/baseinfo 但是浏览器地址栏的url仍然是/blance)
这样的带参数重定向是可行的。

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools