I just have started my first project by using Django 2.0 in which I need to define a URL in a way as:
http://localhost:8000/navigator?search_term=arrow
But I couldn't know how to define a string parameter for a URL in Django 2.0
Here's what I have tried:
From ulrs.py:
from Django.URLs import path from. import views
urlpatterns = [
path('navigator/<str:search_term>', views.GhNavigator, name='navigator'),
]
Any help?
There is no need to define query params in URL. Below url is enough to work.
path('navigator/', views.GhNavigator, name='navigator')
Let you called URL http://localhost:8000/navigator/?search_term=arrow then you can get search_term by request.GET.get('search_term').
Request: GET
http://localhost:8000/navigator?search_term=arrow
urls.py
urlpatterns = [
path('navigator/', views.GhNavigator, name='navigator'),
]
views.py
search_term = request.GET.get('search_term', None)
Related
How can I define the URL pattern so that I can pass to an URL as many parameters as I want? I really researched the documentation and other stackoverflow questions but I didn't found something similar to that. I need this to work as a filter for an ecommerce website.
I would like to achieve something like this:
urlpatterns = [
path('test/<str:var1>-<str:var2>/<str:var3>-<str:var4>/...', views.test, name='test'),
]
And in my view function I would define it like that:
def test(request, *args, **kwargs):
# Do whatever you want with kwargs
return HttpResponse('Test')
I think this is a wrong way to make a path, if you want to use it as a filter instead of using it in the path you should use url parameters as a get request.
But if you insist on doing that you can use the Regular expressions 're_path'
# urls.py
from django.urls import path, re_path
from django.conf.urls import url
from myapp import views
urlpatterns = [
re_path(r'^test/(?P<path>.*)$', views.test, name='test'),
# or use url instead it's same thing
url(r'^test/(?P<path>.*)$', views.test, name='test'),
]
Have you considered using query_params?
That is path('test', views.test, name='test')
URL: /test/?filter=asd....
Then access it via the request in the view:
def test(request):
params = request.GET.get('filter', None)
return HttpResponse()
See if you can work out your problem like that :)
I have a url like this get method in a browser it says 404 page not found error
http://localhost:8000/getup/downloadrange/ef46219d-7b33-4bdc-aab1-c3bf073dca0e/datebottom=2019-10-10&datetop=2020-10-01/
My urls.py is like this
path(
"downloadrange/<uuid:id>/(?P<datebottom>\d{4}-\d{2}-\d{2})&(?P<datetop>\d{4}-\d{2}-\d{2})/$",
views.getup,
name="getup",
),
The url pattern is not found for this. Kindly help me in this regard
my views.py
def getup(request, id, dateone, datetwo):
queryset_two = (
getup.objects.filter(process_id=id)
.filter(created_on__date__range=[dateone, datetwo])
)
return render_to_csv_response(qs)
The valid url you are expecting according to your configuration is:
http://localhost:8000/getup/downloadrange/ef46219d-7b33-4bdc-aab1-c3bf073dca0e/2019-10-10&2020-10-01/
NOT
http://localhost:8000/getup/downloadrange/ef46219d-7b33-4bdc-aab1-c3bf073dca0e/datebottom=2019-10-10&datetop=2020-10-01/
Your views have that capacity to take datebottom and datetop automatically from your url if it is valid.
Edit
As you are using path, the url confs is different. So, we will change from path to re_path to support regex:
from django.urls import re_path
re_path(
"downloadrange/(?P<id>[0-9a-f-]+)/(?P<datebottom>\d{4}-\d{2}-\d{2})&(?P<datetop>\d{4}-\d{2}-\d{2})/$",
views.getup,
name="getup",
),
I am learning django and I am trying to add a new url to '/'
here is my urls.py:
from django.contrib import admin
from django.urls import path
from blog import views as blog_views
urlpatterns = [
path(r'^$', blog_views.index),
path('admin/', admin.site.urls),
]
and here is the index method from blog/views:
def index(request):
return HttpResponse("Hey There")
But when I go to '/' route, I get a 404 response. It seems that it fails at import from blog import views as blog_views in urls.py.
What is going wrong?
Here is my project structure:
Here is the error I get:
In Django 2.x there is a path function instead of django's 1.x url function
the path function doesn't accept Regular Expressions it only accepts normal text
So to make a url for the home page with path function you only need to write your url path like this :
urlpatterns = [
path('', blog_views.index), # http://localhost/
path('admin/', admin.site.urls),
]
read more about Django 2.x urls here:
https://docs.djangoproject.com/en/dev/ref/urls/
urls.py file which is in 1.1 version of Django :-
urlpatterns = patterns('ecomstore.catalog.views',
(r'^category/(?P<category_slug>[-\w]+)/$','show_category',
{'template_name':'catalog/category.html'},'catalog_category'),
)
which I understood that first argument id prefix to all views. next argument is url which has four argument one is url string(regex),second is view , third is dict passing template name and fourth is location of category.
How to write it in Django 1.10
is following it correct way:-
from django.conf.urls import url
from ecommstore.catalog.views import *
urlpatterns = [
url(r'^category/(?P<category_slug>[-\w]+)/$','show_category',
{'template_name':'catalog/category.html'},'catalog_category'),
]
You're almost there. You've imported the view, but you're still passing in a string as the view instead of the view function itself:
urlpatterns = [
url(r'^category/(?P<category_slug>[-\w]+)/$', show_category,
{'template_name':'catalog/category.html'}, 'catalog_category'),
]
I'd like to redirect url pattern with variables from urls.py.
I refer other stackoverflow solution, but I don't know when url having a variable like following code.
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns(
url(
r'^permalink/(?P<id>\d+)/foo/$',
RedirectView.as_view(url='/permalink/(?P<id>\d+)/')
),
)
With this code, django will redirect /permalink/1/foo/ to /permalink/(?P<id>\d+)/, not the /permalink/1/.
Is there any solution without using views.py?
Of course I know solution using controller, but I wonder is there any simpler solution with using url pattern.
Passing url='/permalink/(?P<id>\d+)/' to RedirectView will not work, because the view does not substitute the named arguments in the url.
However, RedirectView lets you provide the pattern_name instead of the url to redirect to. The url is reversed using the same args and kwargs that were passed for the original view.
This will work in your case, because both url patterns have one named argument, id.
urlpatterns = [
url(r'^permalink/(?P<id>\d+)/foo/$',
RedirectView.as_view(pattern_name="target_view"),
name="original_view"),
url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]
If the target url pattern uses other arguments, then you can't use url or pattern_name. Instead, you can subclass RedirectView and override get_redirect_url.
from django.core.urlresolvers import reverse
from django.views.generic import RedirectView
class QuerystringRedirect(RedirectView):
"""
Used to redirect to remove GET parameters from url
e.g. /permalink/?id=10 to /permalink/10/
"""
def get_redirect_url(self):
if 'id' in self.request.GET:
return reverse('target_view', args=(self.request.GET['id'],))
else:
raise Http404()
It would be good practice to put QuerystringRedirect in your views module. You would then add the view to your url patterns with something like:
urlpatterns = [
url(r'^permalink/$', views.QuerystringRedirect.as_view(), name="original_view"),
url(r'^permalink/(?P<id>\d+)/$', views.permalink, name="target_view"),
]