Within my model I have the following that links through to a detail page.
def get_absolute_url(self):
kwargs = {
'job_id': self.job_id,
'slug': self.slug,
}
return reverse('job_detail', kwargs=kwargs)
Everything works fine with this.
Within my urls.py I have the following:
url(r'^$', views.JobListView.as_view(), name='job_list'),
url(r'^(?P<job_id>[-\w]*)/(?P<slug>[-\w]*)/$', views.JobDetail.as_view(), name='job_detail'),
url(r'^(?P<job_id>[-\w]*)/(?P<slug>[-\w]*)/job_application/$', views.job_application, name=‘job_application'),
Then within my views.py the following:
def job_application(request, job_id, slug):
# Retrieve job by id
job = get_object_or_404(Job, job_id=job_id)
“””Other code here””””
However, I am having problems linking to this from my template in my detail page.
What structure should I be using to link to the job application from the job detail page?
I can access the url by going to
http://localhost:8000/jobs/1/name-of-job/job_application/
Any advice would be appreciated
Assuming I’m understanding your question correctly, all you need to do is reference get_absolute_url in your template.
Something like so:
<a href=”{{ job_application.get_absolute_url }}”>{{ job_application }}</a>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/0e6db077-52a8-4f23-862c-888ae0995e91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I had tried that but unfortunately it doesn't
work but thanks anyway.
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e1e214b640c4d4f8c7c5e1d5cf0850b%40ISS1.ISS.LOCAL.
Hi David,
Please make sure you have a get_absolute_url method on your JobApplication model in order for that to work and that you passing job_application in through the context in the template.
If you don’t have that method, you could use the {% url %} tag, but you would have to provide the pk and slug as arguments.
<a href=”{% url ‘job_application’ pk=job_application.job_id slug=job_application.slug %}”>{{ job_application }}</a>
Good luck!
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5412da70-2ee1-9259-1f28-71e85e6e4ab9%40gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e6db077-52a8-4f23-862c-888ae0995e91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e1e214b640c4d4f8c7c5e1d5cf0850b%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5412da70-2ee1-9259-1f28-71e85e6e4ab9%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1ef6a4df22b3475fa5dff71b074e4e54%40ISS1.ISS.LOCAL.
Hi David,
Thank you for the clarification, but it doesn’t change my response much.
Use the {% url %} tag in your detail view template.
<a href=”{% url ‘job_application’ pk=job.job_id slug=job.slug %}”>Job Application Form</a>
I thought you had a JobApplication model, but you can use the Job model just as above. Use the job object you passed into the detail view to obtain the job_id and slug of the job.
Thank you,
Matthew
From: 'David Turner' via Django users [mailto:django...@googlegroups.com]
Sent: Wednesday, February 1, 2017 1:44 AM
To: django...@googlegroups.com
Subject: Re: Link to urls containing slug
Hi Matthew
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e6db077-52a8-4f23-862c-888ae0995e91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e1e214b640c4d4f8c7c5e1d5cf0850b%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at
https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/5412da70-2ee1-9259-1f28-71e85e6e4ab9%40gmail.com.
For more options, visit
https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at
https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/1ef6a4df22b3475fa5dff71b074e4e54%40ISS1.ISS.LOCAL.
For more options, visit
https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALwQ%2B-vEGUqdE4MHdV5rHxfNdfScFD%2BN5HP49QuUjW%2Br0Z%3DTbw%40mail.gmail.com.
Hi David,
Thank you for the clarification, but it doesn’t change my response much.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e6db077-52a8-4f23-862c-888ae0995e91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e1e214b640c4d4f8c7c5e1d5cf0850b%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5412da70-2ee1-9259-1f28-71e85e6e4ab9%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1ef6a4df22b3475fa5dff71b074e4e54%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALwQ%2B-vEGUqdE4MHdV5rHxfNdfScFD%2BN5HP49QuUjW%2Br0Z%3DTbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/hlWrp8tzpXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/23be504a06ab4656bc92621116329779%40ISS1.ISS.LOCAL.
As a side note:
On Tuesday 31 January 2017 06:46:05 'dtdave' via Django users wrote:
> url(r'^(?P<job_id>[-\w]*)/(?P<slug>[-\w]*)/$',
> views.JobDetail.as_view(), name='job_detail'),
Why use this dual structure? Id already identifies the object to retrieve, nothing else is needed.
Slug, ideally, is also unique, so pick one, not both.
--
Melvyn Sopacua