In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In this chapter, we will use some clever tricks for view and URL configuration.
[streamlined function Import (streamlining)]
Method 1: traditional method
From django.conf.urls.defaults import * from mysite.views import hello, current_datetime, hours_aheadurlpatterns = patterns (', (r'^ hello/$', hello), (r'^ time/$', current_datetime), (r'^ time/plus/ (\ d {1m 2}) / $', hours_ahead),)
Method 2: import views function, which is easy to maintain and aim at the view module of import
From django.conf.urls.defaults import * * from mysite import views**urlpatterns = patterns (', (r'^ hello/$', views.hello'), (r'^ time/$', views.current_datetime), (r'^ time/plus/ (d {1m 2}) / $', views.hours_ahead),)
Method 3: import module name and view function name, enclosed in quotation marks
From django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ hello/$', 'mysite.views.hello'), (r' ^ time/$', 'mysite.views.current_datetime'), (r' ^ time/plus/ (d {1m 2}) / $', 'mysite.views.hours_ahead'),)
Method 4: extract the public view prefix, do not put a period after the prefix and before the view string, django will automatically deal with it
From django.conf.urls.defaults import * urlpatterns = patterns ('mysite.views', (r' ^ hello/$', 'hello'), (r' ^ time/$', 'current_datetime'), (r' ^ time/plus/ (d {1m 2}) / $', 'hours_ahead'),)
Method 4 (2): what if our URLconf doesn't have a common prefix? As follows
From django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ hello/$', 'mysite.views.hello'), (r' ^ time/$', 'mysite.views.current_datetime'), (r' ^ time/plus/ (\ d {1pm 2}) / $', 'mysite.views.hours_ahead'), (r' ^ tag/ (\ w +) / $', 'weblog.views.tag'),)
Solution: the entire framework focuses on variables at the urlpatterns module level, and patterns returns objects that can be added.
From django.conf.urls.defaults import * urlpatterns = patterns ('mysite.views', (r' ^ hello/$', 'hello'), (r' ^ time/$', 'current_datetime'), (r' ^ time/plus/ (\ d {1m 2}) / $', 'hours_ahead'),) urlpatterns + = patterns (' weblog.views', (r'^ tag/ (\ w +) / $', 'tag'),)
Special case: django debug mode to modify the behavior of URLconf technology, link debuginfo is only valid when the DEBUG configuration item is True.
From django.conf import settingsfrom django.conf.urls.defaults import * from mysite import viewsurlpatterns = patterns (', ) If settings.DEBUG: urlpatterns + = patterns ('', (r'^ debuginfo/$', views.debug),)
[naming matching pattern of url]
Method 1: use named groups, such as the corresponding relationship between position parameters and keyword parameters in the python function, and its syntax (? P matching pattern).
# # traditional method from django.conf.urls.defaults import * from mysite import viewsurlpatterns = patterns (', (r'^ articles/ (\ d {4}) / $', views.year_archive), (r'^ articles/ (\ d {4}) / (\ d {2}) / $', views.month_archive),) # # use the named group from django.conf.urls.defaults import * from mysite import viewsurlpatterns = patterns ('' (r'^ articles/ (? P\ d {4}) / $', views.year_archive), (r'^ articles/ (? P\ d {4}) / (? P\ d {2}) / $', views.month_archive),)
In order to distinguish between them, take the request / archive/2016/12 as an example, how are the functions called?
The former: month_archive (request, '2016','12')
The latter: month_archive (request, year='2016', month='12')
Disadvantages: although this makes it more readable and more accurate, it is less redundant; and both named and unnamed group formats are not allowed in an URLconf schema, and the priority is named group > unnamed group > keyword to pass additional parameters.
[pass additional parameter information]
In the view function we wrote, we will find that there are many view functions that are similar, but different. How can we make it more concise? the original template is as follows:
# urls.pyfrom django.conf.urls.defaults import * from mysite import viewsurlpatterns = patterns (', (r' ^ foo/$', views.foo_view), (r' ^ bar/$', views.bar_view),) # views.pyfrom django.shortcuts import render_to_responsefrom mysite.models import MyModeldef foo_view (request): m_list = MyModel.objects.filter (is_new=True) return render_to_response ('template1.html') {'masked listings: m_list}) def bar_view (request): m_list = MyModel.objects.filter (is_new=True) return render_to_response (' template2.html', {'masked listings: m_list})
Method one: add if to judge, the disadvantage is that url is coupled to the code, and if you change url, you have to change the view function.
# views.pyfrom django.shortcuts import render_to_responsefrom mysite.models import MyModeldef foobar_view (request, url): m_list = MyModel.objects.filter (is_new=True) if url = = 'foo': template_name =' template1.html' elif url = = 'bar': template_name =' template2.html' return render_to_response (template_name, {'masked listings: m_list})
Method 2: URLconf contains the third position parameter: keyword parameter
# urls.pyfrom django.conf.urls.defaults import * from mysite import viewsurlpatterns = patterns (', (r'^ foo/$', views.foobar_view, {'template_name':' template1.html'}), (r'^ bar/$', views.foobar_view, {'template_name':' template2.html'}),) # views.pyfrom django.shortcuts import render_to_responsefrom mysite.models import MyModeldef foobar_view (request Template_name): m_list = MyModel.objects.filter (is_new=True) return render_to_response (template_name, {'masked listings: m_list})
For example: we want to access the url of the following rule
/ mydata/jan/01/
/ mydata/jan/02/
/ mydata/jan/03/
#...
/ mydata/dec/30/
/ mydata/dec/31/
We can set the URLconf and view functions as follows:
Urlpatterns = patterns (''
(r'^ mydata/ (? P\ w {3}) / (? P\ d\ d) / $', views.my_view)
)
Def my_view (request, month, day):
#....
But when we want to add access to a / mydata/birthday/ url, normally we give him a view function, but we can use the extra parameters passed above to solve the problem
Urlpatterns = patterns (''
(r'^ mydata/birthday/$', views.my_view, {'month':' jan', 'day':' 06'})
(r'^ mydata/ (? P\ w {3}) / (? P\ d\ d) / $', views.my_view)
)
[include other URLconf]
Sometimes we want our code to be used for multiple django sites, so we have to consider dealing with our URLconf in an inclusive manner.
From django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ weblog/', include ('mysite.blog.urls')), (r' ^ photos/', include ('mysite.photos.urls')), (r' ^ about/$', 'mysite.views.about'),)
Note: there is no $sign in the url containing other urls, but it contains /, which means that when django encounters include, it will truncate the matching URL and send the rest of the string to the included URLconf for further processing.
For example, our access / weblog/2007/ weblog is matched by this URLconf, and / truncated 2007 is given to the urls in the included URLconf.
1. Capture parameters and include
# root urls.pyfrom django.conf.urls.defaults import * urlpatterns = patterns (', (r' ^ (? P\ w+) / blog/', include ('foo.urls.blog')),) # foo/urls/blog.pyfrom django.conf.urls.defaults import * urlpatterns = patterns (', (r' ^ $', 'foo.views.blog_index'), (r' ^ archive/$',' foo.views.blog_archive'),)
In this case, the captured username variable is passed to the included URLconf, which in turn is passed to each view function in that URLconf.
2. Additional URLconf and include
As mentioned above, URLconf has a parameter in the third position, which is expressed in a dictionary, that is, the following two configurations are equivalent:
# urls.pyfrom django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ blog/', include ('inner'), {' blogid': 3}),) # inner.pyfrom django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ archive/$', 'mysite.views.archive'), (r' ^ about/$', 'mysite.views.about'), (r' ^ rss/$', 'mysite.views.rss') ) # urls.pyfrom django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ blog/', include ('inner')),) # inner.pyfrom django.conf.urls.defaults import * urlpatterns = patterns (', (r'^ archive/$', 'mysite.views.archive', {' blogid': 3}), (r'^ about/$', 'mysite.views.about', {' blogid': 3}), (r'^ rss/$') 'mysite.views.rss', {' blogid': 3}),)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.