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.
Related
I have been spending the better part of my week learning Django to build my website and I've stumbled upon an issue I can't seem to get working. I wish to resolve a URL string for a path by including with that path a particular name and then being able to reference that name down the line. This works fine until I change the route in path to use the include method.
First Attempt:
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls'), name='testapp'),
path('about/', views.about, name='about'),
]
Now when I call {% url 'about' %} from my template html file I get back '/about/' as expected but when I try and call {% url 'testapp' %} I get a NoReverseMatch exception instead of getting '/testapp/'. After digging through the documentation I stumbled upon this example that shows path with include using a namespace instead so I adapted the above a bit.
Second Attempt:
# from mysite/urls.py (adapted from before)
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls', namespace='testapp')),
path('about/', views.about, name='about'),
]
#from testapp/urls.py
from django.url import include, path
from . import views
app_name = 'testapp_name'
urlpatterns = [
path('', views.index, name='testapp_index'),
path('directory/', views.directory, name='testapp_directory'),
]
Now from the previous example I try using the namespace in lieu of a name {% url 'testapp' %} and I again get the same NoReverseMatch exception however using the namespace and name from my included url {% url 'testapp:directory' %} does work giving me '/testapp/directory/'.
I know there's some concept I'm not getting or something I'm overlooking but I'm just running around in circles at this point and would really appreciate any help somebody could afford me.
If it helps I'm trying to get the path so that I can use it in a navigation bar to highlight the currently activated tab. I'm also not hardcoding the paths as I'm trying to keep it DRY although at this point if I can't get it done I might have to but I'm hoping someone has a much better idea of what they're doing and could point me in a helpful direction. I appreciate all assistance and thank you!
The problem is that testapp is not a single view: it is an include(..), so it encapsulates a collection of views:
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls'), name='testapp'),
path('about/', views.about, name='about'),
]
It is not said that this collection contains a view at all, or it can contain multiple. Even if it only contains a single view, then it would be unsafe since you can later change your mind, and add an extra view.
If there are two or more views, then how will you decide what view (and therefore URL) to take? If the include(..) has two views: the "homepage" and the profile page, then this makes a significant difference.
You thus should refer to a real name, and whether you give the include(..) a namespace in the include(..) is irrelevant:
{% url 'testapp_name:testapp_index' %} <!-- first attempt -->
{% url 'testapp:testapp_index' %} <!-- second attempt -->
To reference the name of a real view.
{% url 'testapp_name:testapp_index' %}
or
{% url 'testapp_name:testapp_directory' %}
as you are using app_name in the urls file, you need to mention it with the name of the view
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.
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>
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.
I have completed the tutorial for Haystack w/ Whoosh and re-read it about 6 times in the past few hours and cannot seem to figure out what my issue is. Basically, everything seemed to installed correctly, however when I visit the /search URL all I see is my base template without the search template (no input box/header/etc). This is my urls.py at the application level:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^search/', include('haystack.urls')),
url(r'a/list/$', views.admin_list, name='admin_list'),
url(r'a/list/(?P<list_id>\d+)/edit/$', views.admin_edit, name='admin_edit'),
)
The template lives in myapp/templates/search/search.html. I have added nothing to my views.py, because the tutorial didn't go over adding anything to that file. What am I missing here?
What is content of the data template file ( appname_text) file , if you are keeping document =true in search_index.py file your data template should have the fields that are to be searched .
data template for will be something like.
{{ object.foo }}
{{ object.bar }}