how to get url for django url include without name - python

I have the following main site urls.py:
urlpatterns = patterns('',
(r'^search/', include('haystack.urls')),
)
I need to call this from a link, but this type of entry has no name I can with something like:
Go To Search
How can I call this url entry?

Django haystack urls are already named:
https://github.com/toastdriven/django-haystack/blob/master/haystack/urls.py
So you can use:
<a ref={% url "haystack_search" %}>search page</a>

you can do:
<a href='/appname/search'>Go To Search</a>

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 with parameters not working

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.

DJango - NoReverseMatch Error

That is the exception value:
Reverse for '' with arguments '()' and keyword arguments '{'id': 1}' not found. 0 pattern(s) tried: []
index.html
<p>Estudiante: {{student.stduent_name}}</p>
The link should go to a route like this "127.0.0.1:8000/polls/1/". The route works fine out of the link.
views.py
def student_detail(request, id):
student = get_object_or_404(Student, id=id)
return render(request, 'polls/student_detail.html', {'student': student})
urls.py
urlpatterns = [
url(r'^$', views.index),
url(r'^polls/(?P<id>[0-9]+)/', views.student_detail),
]
Images:
Error details
Route tree
The first argument to the url template tag is the "url name". You need to specify a name when you define the route, something like:
url(r'^polls/(?P<id>[0-9]+)/', views.student_detail, name='student-detail'),
and then update your template to use it like:
{% url 'student-detail' id=student.id %}
See the documentation on the url template and url names.
The template code in the exception is different to the template code you've pasted in your question. The exception indicates your template tag looks like:
{% url polls.views.student_detail id=student.id %}
Note the missing quotes which is consistent with the exception. Without the quotes django is attempting to resolve polls.views.student_detail as a variable instead of passing it as a string to the template tag. Since it can't resolve you're passing a blank string to your template tag.
You are invoking an url by name but there's no such named url in the urls.py file.
your urlpatterns should be:
urlpatterns = [
url(r'^$', views.index),
url(r'^polls/(?P<id>[0-9]+)/', views.student_detail, name='student_detail'),
]
And then in your template:
<p>Estudiante: {{student.stduent_name}}</p>
notice that you don't need to explicitly pass the parameter name, Django transforms each parameter separated by space in the respective parameter specified in the url regex pattern.

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