django-urls

How to handle request.GET with multiple variables for the same parameter in Django

时光怂恿深爱的人放手 提交于 2019-11-27 00:39:34
In a Django view you can access the request.GET['variablename'] , so in your view you can do something like this: myvar = request.GET['myvar'] The actual request.GET['myvar'] object type is: <class 'django.http.QueryDict'> Now, if you want to pass multiple variables with the same parameter name, i.e: http://example.com/blah/?myvar=123&myvar=567 You would like a python list returned for the parameter myvar , then do something like this: for var in request.GET['myvar']: print(var) However, when you try that you only get the last value passed in the url i.e in the example above you will get 567 ,

Is it better to use path() or url() in urls.py for django 2.0?

青春壹個敷衍的年華 提交于 2019-11-27 00:01:19
In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I've seen other examples on youtube of this. e.g. from django.contrib import admin from django.urls import include from django.conf.urls import url urlpatterns = [ path('admin/', admin.site.urls), url(r'^polls/', include('polls.urls')), ] #and in polls/urls.py urlpatterns = [ url(r'^$', views.index, name="index"), ] However, in going through the Django tutorial, they use path() instead e.g.: from django.urls import path from . import views urlpatterns =

Access kwargs from a URL in a Django template

匆匆过客 提交于 2019-11-26 21:14:17
问题 Can I access value of a named argument (from the URL) in a Django template? Like can I access the value of this_name below from a django template? url(r'^area/(?P<this_name>[\w-]+)/$', views.AreaView.as_view(), name="area_list") I could get the whole URL path and break it up but wanted to check if there's a straight forward way to do that, since it already has a name. Passing it down in the context data from the view may be an alternative but not sure if I do need to pass it down since I'd

django url pattern for %20

本秂侑毒 提交于 2019-11-26 20:23:32
问题 In Django what is the url pattern I need to use to handle urlencode characters such as %20 I am using (?P<name>[\w]+) but this only handles alphanumeric characters so % is causing an error 回答1: I was able to make it work using the configuration given below. Check if it will suit your needs. (?P<name>[\w|\W]+) 回答2: If you only want to allow space: (?P<name>[\w\ ]+) 回答3: The best way to do that and allow others chars is using '\s' that is any spaces, tabs and new lines (?P<name>[\w\s]+) 来源:

ImpropyConfiguredError about app_name when using namespace in include()

坚强是说给别人听的谎言 提交于 2019-11-26 19:47:32
问题 I am currently trying out django. I use the namespace argument in one of my include() s in urls.py. When I run the server and try to browse, I get this error. File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include 'Specifying a namespace in include() without providing an app_name ' django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name

How do I set urlpatterns based on domain name or TLD, in Django?

蹲街弑〆低调 提交于 2019-11-26 19:39:05
问题 How do I set urlpatterns based on domain name or TLD, in Django? For some links, Amazon shows url in native language based on its website tld. http://www.amazon.de/bücher-buch-literatur/ ( de : books => bücher ) http://www.amazon.fr/Nouveautés-paraître-Livres/ ( fr : books => Livres ) http://www.amazon.co.jp/和書-ユーズドブッ-英語学習/ ( jp : books => 和書 ) ( the links are incomplete and just show as samples. ) Is it possible to get host name in urls.py? (request object is not available in urls.py) or

add request.GET variable using django.shortcuts.redirect

南笙酒味 提交于 2019-11-26 19:22:50
问题 Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) If I do redirect('url-name', x) I get HttpResponseRedirect('/my_long_url/%s/', x) I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering... 回答1: Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py) I don't know of any way to do this without modifying the urls.py . I don't have complains using HttpResponseRedirect('

Making a Regex Django URL Token Optional

十年热恋 提交于 2019-11-26 19:01:53
问题 You have a URL which accepts a first_name and last_name in Django: ('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/$','some_method'), How would you include the OPTIONAL URL token of title , without creating any new lines. What I mean by this is, in an ideal scenario: #A regex constant OP_REGEX = r'THIS IS OPTIONAL<title>[a-z]' #Ideal URL ('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/OP_REGEX/$','some_method'), Is this possible without creating a new line i.e. ('^(?P<first_name>

How to get the current url name using Django?

我是研究僧i 提交于 2019-11-26 18:58:08
问题 I have to build an url dynamically according to the current url. Using the {% url %} tag is the easiest way to do it, but I need the current url name to generate the new one dynamically. How can I get the url name attached to the urlconf that leads to the current view? EDIT : I know I can manually handcraft the url using get_absolute_url but I'd rather avoid it since it's part of a lecture and I would like to demonstrate only one way to build urls. The students know how to use {% url %} .

Getting Django admin url for an object

送分小仙女□ 提交于 2019-11-26 18:43:07
问题 Before Django 1.0 there was an easy way to get the admin url of an object, and I had written a small filter that I'd use like this: <a href="{{ object|admin_url }}" .... > ... </a> Basically I was using the url reverse function with the view name being 'django.contrib.admin.views.main.change_stage' reverse( 'django.contrib.admin.views.main.change_stage', args=[app_label, model_name, object_id] ) to get the url. As you might have guessed, I'm trying to update to the latest version of Django,