Django URL with parameters not working - python

I'm trying to use pass a parameter to a URL in Django, but I keep getting this error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/%7B%25%20url%20review%20review_id%3D3%20%25%7D
Using the URLconf defined in soundclinic.urls, Django tried these URL patterns, in this order:
[name='index']
login/ [name='login']
register/ [name='register']
create_user [name='create_user']
<int:review_id>/review/ [name='review']
admin/
main/
The current path, {% url review review_id=3 %}, didn't match any of these.
The link I am follow has a tag like above: {% url review review_id=3 %}
I'm not sure what I'm not including right, as it looks like I'm writing out the url tag correctly and urls.py seems to be configured correcely as well.
Manually entering in the URL calls the correct views.py function, so it only has to do with my urls.py file.

As kszl already said, you must use " ' " around your URL name.
{% url 'review' review_id=3 %}
It supposes you have a URL in some url.py that has the name "review".
It seems to be the case though since we can see
<int:review_id>/review/ [name='review']
in your log message.

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".

IDE showing me error on objects keyword in " latest_questions = Question.objects.order_by('-pub_date')[:10] "

Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/'polls/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
^polls/ ^$ [name='index']
^polls/ ^(?P<question_id>[0-9]+)/$ [name='detail']
^polls/ ^(?P<question_id>[0-9]+)/results$ [name='results']he current path, polls/'polls/index.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Your problem is not about IDE, you need to properly configure your urls.py page. It seems like Django couldn't find 'polls/'polls/index.html' address. You need to go to urls.py file and add following line;
path('polls/'polls/index.html/', views.path_to_function, name='index'),

Django 404 on POST request

I'm trying to send a form via POST on my site, however, it appears the first part of the POST URL is being chopped off leading to it not being able to match the correct url.
The form I'm trying to send in the template:
<form action= "{% url 'change_portrait' %}" method="post">
{% csrf_token %}
<h5>Edit Portrait</h5>
<input type="hidden" name="portrait_id" value="{{image.pk}}">
<button type="submit" name="button" class="btn btn-primary">Submit Changes</button>
</form>
which correctly renders to:
<form action= "/AdminDashboard/EditPortraits/ChangePortrait/" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='iCt1xvIc1KZXErhbcMNepk2daHXApGih' />
<h5>Edit Portrait</h5>
<input type="hidden" name="portrait_id" value="14">
<button type="submit" name="button" class="btn btn-primary">Submit Changes</button>
</form>
The action URL is correct, and is where I want the form to be submitted to. However, when the form is submitted I get a 404 with the following message:
Using the URLconf defined in ZachWebsite.urls, Django tried these URL patterns, in this order:
^admin/
^AdminDashboard/
^ ^SundayPortraits/ [name='sunday_portraits']
^ ^Contact/ [name='contact_submission']
^ ^SubmitOrder/ [name='submit_buy_order']
^ ^BuyPrint/ [name='buy_print']
^ ^$ [name='index']
^ ^Login [name='login']
^ ^Logout [name='logout']
^ ^media\/(?P<path>.*)$
The current URL, EditPortraits/ChangePortrait/, didn't match any of these.
Here is my base urls.py
urlpatterns = [
url(r'^AdminDashboard/', include('AdminDashboard.urls')),
url(r'^', include('mainApp.urls'))
]
And AdminDashboard.urls:
urlpatterns = [
url(r'^EditPortraits/ChangePortrait/', views.change_portrait, name='change_portrait'),
url(r'^EditPortraits/', views.edit_portraits, name='edit_portraits'),
url(r'^', views.dashboard, name='dashboard'),
]
For some reason unbeknownst to me submitting the form cuts off the initial '/AdminDashboard/' and only tests against the remaining url 'EditPortraits/ChangePortrait/'. This of course causes it to skip over the first url pattern in the base urls.py (where it should go) and instead match with the catch all second url and subsequently fail to match all the urls in mainApp.urls. I cannot for the life of me figure out why submitting the form cuts off the first section of the action URL. It works as intended on the built in django development server but fails when deployed to the production server.
Any help will be extremely appreciated.
EDIT
mainApp.urls (these are what are shown in the trace but should never be attempted in the first place):
urlpatterns = [
url(r'^SundayPortraits/', views.sunday_portraits, name='sunday_portraits'),
url(r'^Contact/', views.contact, name='contact_submission'),
url(r'^SubmitOrder/', views.submit_buy_order, name='submit_buy_order'),
url(r'^BuyPrint/', views.buy_print, name='buy_print'),
url(r'^$', views.index, name='index'),
url(r'^Login', views.login, name='login'),
url(r'^Logout', views.logout, name='logout')
]
views.change_portrait:
def change_portrait(request):
return HttpResponse('in')
This never shows which means it never gets to the view.
EDIT 2:
It should also be noted that on form submission the url in my browser is correct: i.e. it shows 'mysite.com/AdminDashboard/EditPortraits/ChangePortrait/'. Django for some reason says the current URL is '/EditPortraits/ChangePortrait/'. If I go to that url ('mysite.com/AdminDashboard/EditPortraits/ChangePortrait/') regularly in my browser it correctly loads the page. It's only when I attempt to POST to that page that it fails.
EDIT 3:
The view can be updated to correctly accept the POST variables as follows:
def change_portrait(request):
portrait_id = request.POST.get('portrait_id', None)
return HttpResponse('in')
However the error still persists.
EDIT 4:
view that renders template:
def edit_portraits(request):
if not request.session.get('logged_in', None):
return redirect('../')
portraits = Portrait.objects.filter(isSundayPortrait=True)
context = {'portraits': portraits,
'SundayPortraits': True}
return render(request, 'AdminDashboard/editportraits.html', context)
Moving url(r'^EditPortraits/ChangePortrait/', views.change_portrait, name='change_portrait') to mainApp.urls (where it goes to try to match) results in an updated mainApp.urls:
urlpatterns = [
url(r'^SundayPortraits/', views.sunday_portraits, name='sunday_portraits'),
url(r'^Contact/', views.contact, name='contact_submission'),
url(r'^SubmitOrder/', views.submit_buy_order, name='submit_buy_order'),
url(r'^BuyPrint/', views.buy_print, name='buy_print'),
url(r'^$', views.index, name='index'),
url(r'^EditPortraits/ChangePortrait/$', views.change_portrait, name='change_portrait'),
url(r'^Login', views.login, name='login'),
url(r'^Logout', views.logout, name='logout')
]
mainApp.views.change_portrait:
def change_portrait(request):
if request.method == 'POST':
return HttpResponse('POST')
else:
return HttpResponse('GETTING')
Using the same form as originally shown, doing a get request results in an HttpResponse 'GETTING' as expected. Changing the form to a POST request results in a new stack trace:
Request Method: POST
Request URL: http://www.example.com/EditPortraits/ChangePortrait/
Raised by: mainApp.views.change_portrait
Using the URLconf defined in ZachWebsite.urls, Django tried these URL patterns, in this order:
^admin/
^AdminDashboard/
^ ^SundayPortraits/ [name='sunday_portraits']
^ ^Contact/ [name='contact_submission']
^ ^SubmitOrder/ [name='submit_buy_order']
^ ^BuyPrint/ [name='buy_print']
^ ^$ [name='index']
^ ^EditPortraits/ChangePortrait/ [name='change_portrait']
^ ^Login [name='login']
^ ^Logout [name='logout']
^ ^media\/(?P<path>.*)$
The current URL, ChangePortrait/, didn't match any of these.
Moving to mainApp.urls (where it should catch it) now cuts off the first part of that url 'EditPortrait/' and now only tests against 'ChangePortrait/'
In your AdminDashboard.urls:
All your regular expressions missed the $
Note: your base urls.py doesn't need to have the $ at the end
url(r'^EditPortraits/ChangePortrait/',
views.change_portrait, name='change_portrait')
the good one is
url(r'^EditPortraits/ChangePortrait/$',
views.change_portrait, name='change_portrait')
After more investigation and testing, I seem to have figured out the problem. Turns out to be a problem with how Django is handling script_name and path_info. Because the form is posting to a sub url, the first part of the url '/AdminDashboard/' was being cut off and saved as the script_name. The remainder of the url, 'EditPortrait/ChangePortrait/' was being saved as the path_info. Django then attempted to match path_info against urls. Obviously it didn't match anything as it was missing the first part of the url that had been saved in script_name. Changing this line:
self.path_info = path_info
in django.core.handlers.wsgi.py to:
self.path_info = script_name + path_info
fixed the problem and is now working properly.
You say "It works as intended on the built-in Django development server but fails when deployed to the production server".
To me, this indicates a problem with the deployment on the production server that does not exist on the development server. For example, maybe on the production server, you are using a web server such as Apache or NGINX that you are not using on development.
I would check and double check any infrastructure such as web servers, load balancers, etc that gets the request before Django does, and make sure it isn't rewriting the path or otherwise passing incorrect configuration to Django.

Django URL names use in Templates

Right now in my templates I hardcore the links in my navigation like the following:
`base.html`
About
Contact
<!-- etc -->
In my urls.py
urlpatterns = patterns('',
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name='about'),
url(r'^contact/$', TemplateView.as_view(template_name='pages/contact.html'), name='contact'),
)
Is there a way that in my base.html file reference the urlpatterns from urls.py so that anytime I change those, it reflects everywhere across my pages and templates?? Something like
<!-- what I would like to do -->
About
About
This is why url tag was invented:
Returns an absolute path reference (a URL without the domain name)
matching a given view function and optional parameters.
If you’re
using named URL patterns, you can refer to the name of the pattern in
the url tag instead of using the path to the view.
This basically means that you can use url names configured in the urlpatterns in the url tag:
About
Contact
This gives you more flexibility. Now any time the actual url of about or contact page changes, templates would "catch up" the change automagically.
Use Django's url template tag (docs here) with your named url pattern.
Example:
About
Contact
Note that the name you put in ' ' will be the name of the particular url pattern in your urls.py. Also, if this is the urls.py file in an app (that is, it gets routed initially from the base urls.py), you'll need to include the namespacing of the app.

how to use the href attribute in django templates

When I try to use a link in my Django template from /appname/index/ to get to /appname/detail/### I am instead getting to /appname/index/detail/### which is not what I'm trying to get so my app can't find it in the urlconf of course.
First the urls.py line for the detail page
url(r'detail/(?P<jobID>\d+)/$', 'appname.views.detail')
Additionally, the root urlconf
urlpatterns = patterns('',
url(r'^appname/', include('appname.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Next the template code trying to get there
{% for job in jobList %}
{{ job.name }}
I'm not sure what else might be applicable information, just ask if you would like to see something else. I also tried :
{{ job.name }}
But that didn't work either. Thank you in advance for any help.
Add / at start in href:
{{ job.name }}
And for the url tag to work you need to do it like this:
{{ job.name }}
From my experience, as long as you have defined the url of the page the href tag should lead to in urls.py, include the absolute path in the format below.
Site name: yyyy.com
Url of page to redirect to in urls.py: yyyy.com/signup/
Sample link: Signup
You will notice that what goes inside the href tag is sort of appended to the current url. For more dynamic links, you can use some python as of DTL guidelines.
Suppose u are at
myapp/detail
and u want to go at page
myapp/anything
U shoud do this
Page
For django templates
Url.py File
app_name = 'app_name'
path('url/', TestView.as_view(), name='the_url'),
path('url//<int:id>', TestView.as_view(), name='the_url'),
templates(html.py)
Test
Test
I got the same problem, new address is being added after the "/" URL which is not recognised.
Can be solved by adding "/" at the address you are giving at the href attribute.
ex: href:services is wrong
href:/services is to be used to go to services page.

Categories

Resources