Passing parameters through django url error - python

I have been trying to load my localhost:8000/streamers/1234 however there is a bug in my urls that I cannot seem to fix. Iv tried both patterns below and I keep getting the error:
Django tried these URL patterns, in this order:
^streamers/(?P[0-9]+)/$ [name='streamer']
The current path, streamers/34/, didn't match any of these.
urlpatterns = [
#path(r'^streamers/<int:id>/', views.streamer, name='streamer'),
url(r'^streamers/(?P<id>[0-9]+)/$', views.streamer, name='streamer'),
]

if "views.streamer" is a class based view, use:
path(r'^streamers/<int:id>/', views.streamer.as_view(), name='streamer'),
Notice the "as_view()" after views.streamer.

Related

django can't find url even though it is there

I have an issue where I try to go to my redirect page and get a NoReverseMatch when though the URL is there? Any idea how to fix this?
I have checked that the "schema" url works and it correctly supplies the openapi schema, but the other page simply can't understand the url.
URLS:
urlpatterns = [
path("schema/", SpectacularAPIView.as_view(), name="schema"),
# Optional UI:
path("docs/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
]
Errors:
For reverse url pathing, you have to use {% url api:schema %}. It's specified as namespace next to include('api.urls') or inside app urls, just above urlpatterns - like app_name = "api".

Django url passing more than one parameter

in my django project i create in urls.py file an entry like this one:
....
url(r'^pd/<str:df>/<str:dt>/<int:v_id>/<str:interval>', calc_q),
...
because i need to pass different params to my calc_q function.
Well when i start my django project and try to call my url:
http://127.0.0.1:8000/pd/2021-06-27/2021-06-29/17/15min/
i get an error:
...
^pd/str:df/str:dt/int:v_id/str:interval
...
The current path, pd/2021-06-27/2021-06-29/17/15min/, didn't match any of these.
Why djngo cannot find my url in url list?
So many thanks in advance
you are mixing the regex syntax that url(…) and re_path(…) [Django-doc] use with the syntax for a path(…) [Django-doc]. You thus work with a path like:
from django.urls import path
urlpatterns = [ScopedTypeVariables
# …,
path('pd/<str:df>/<str:dt>/<int:v_id>/<str:interval>/', calc_q),
# …
]

URL Regular Expression mismatch

I am trying to learn Django and I am currently stuck in an issue.
I created an app Contact and run the server, I get the error.
The error page displayed by server:
The urls.py file in the app Contact
urls.py in conatct
When the pattern in urls.py is
urlpatterns =[url(r'^$', views.form, name ='form')]
it works properly, but not with other pattern shown in the picture
Your help would be greatly appreciated.
The Page not found error message tells you what went wrong: For the URL (/contact) you requested, Django was unable to find a suitable view. Because you have debugging enabled, you get some information, including a list of registered views.
First things first: You probably have url(r'^contact/', include('contact.urls')) somewhere in your top level urls.py. This makes the URLs defined in the contact/urls.py available under the prefix /contact.
With
urlpatterns = [
url(r'^form/', views.form, name='form'),
]
in contact/urls.py you are telling Django that you want urls starting with contact/form/ to be handled by views.form.
Consequently, when you access http://localhost:8000/contact/ in your browser, there is no view associated with that URL, hence the 404. Your view is reacting to to http://localhost:8000/contact/form, not http://localhost:8000/contact.
When you change the URL pattern to
urlpatterns = [
url(r'^$', views.form, name='form'),
]
you modify the URL views.form reacts to.

Django how to define default app for home URL?

I'm learning Django, and so far I always had to use URL's like
projectname/appname/viewname
but what if I don't want appname to appear in the URLs for the "default" app, how can I configure my urls so that
projectname/viewname
will load the view viewname from my default app?
P.S. : Of course my primary goal is to be able to use the URL projectname/ to load the default view for the default app.
Details
Currently my ProjectName/urls.py has this:
urlpatterns = patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp1/', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),
url(r'^myapp2/', include('myapp2.urls', namespace='myapp2', app_name='myapp2')),
)
so when I deploy my project to Heroku, and visit myproject.heroku.com, I get the error :
Page not found (404)
Request Method: GET
Request URL: https://myproject.herokuapp.com/
Using the URLconf defined in MyProject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^myapp1/
^myapp2/
I know this is supposed to be, but how do I fix (or hack) this to get myproject.heroku.com to work?
If not possible, how can I redirect the homepage to myproject/myapp1/defaultview ?
Thanks in advance !
my app's urls.py looks like this :
urlpatterns = patterns('myapp1.views',
url(r'^view1/$', 'view1', name='view1'), # the default view
url(r'^view2/(?P<oid>\d+)/(?P<pid>\d+)/$', 'view2', name='view2'),
)
Edit
After trying #Wallace 's suggestion url(r'^$', include('myapp1.urls', namespace='myapp1', app_name='myapp1')), and hitting the homepage, I now get the error:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^static/(?P<path>.*)$
^admin/
^$ ^view1/$ [name='view1']
^$ ^view2/(?P<oid>\d+)/(?P<pid>\d+)/$ [name='view2']
^myapp2/
Tried changing your project urls.py with:
url(r'', include('myapp1.urls', ...)
This will include all urls from myapp1.urls where they all append to /.
The reason why r'^$' won't work is because the regex ends with $ which means there can only be 1 x url /, and because your app1.urls only has 2 urls defined and without a / or ^$ equivalent, the url resolver will then fail.
But be aware of url clashes, if your project has a ^view1/$ url it will clash to your app1's view1 for example.
Try not including your appname in the regular expression.
url(r'', include('myapp1.urls', namespace='myapp1', app_name='myapp1')),

Django URLconf: How to use captured params in include's RedirectView?

I have a parent URLconf:
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
(r'^main/(?P<name>[^/]+)/(?P<region>[^/]+)/(?P<id>[^/]+)/', include('foo')),
)
And a child URLconf (included in the parent) that includes a redirect:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^view/$', RedirectView.as_view(url='/main/%(name)s/%(region)s/%(id)s/detail')),
)
(Essentially, I'm trying to redirect a path that looks like /main/product/1/134/view to a path that looks like /main/product/1/134/detail.)
The Django documentation says that "An included URLconf receives any captured parameters from parent URLconfs."
But when I try access /main/product/1/134/view, I get a KeyError because name isn't recognized.
Is there some other way that I have to reference the received captured parameters in the RedirectView?
Note: I don't get an error when I do the whole thing in the parent URLconf:
urlpatterns = patterns('',
(r'^main/(?P<name>[^/]+)/(?P<region>[^/]+)/(?P<id>[^/]+)/view/$', RedirectView.as_view(url='/main/%(name)s/%(region)s/%(id)s/detail'))
)
This section of the docs suggests that you should be using two percent signs instead of one:
The given URL may contain dictionary-style string formatting, which will be interpolated against the parameters captured in the URL. Because keyword interpolation is always done (even if no arguments are passed in), any "%" characters in the URL must be written as "%%" so that Python will convert them to a single percent sign on output.
So in your case, try:
url(r'^view/$', RedirectView.as_view(url='/main/%%(name)s/%%(region)s/%%(id)s/detail')),
It might be cleaner to use the pattern_name argument instead of url. The args and kwargs will be used to reverse the new url.
url(r'^view/$', RedirectView.as_view(pattern_name='name_of_url_pattern_to_redirect_to')),

Categories

Resources