Link to urls containing slug

39 views
Skip to first unread message

dtdave

unread,
Jan 31, 2017, 9:46:05 AM1/31/17
to Django users

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

Matthew Pava

unread,
Jan 31, 2017, 9:59:01 AM1/31/17
to django...@googlegroups.com

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.

David Turner

unread,
Jan 31, 2017, 10:09:58 AM1/31/17
to django...@googlegroups.com

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.

Matthew Pava

unread,
Jan 31, 2017, 11:33:34 AM1/31/17
to django...@googlegroups.com

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!

David Turner

unread,
Feb 1, 2017, 2:44:51 AM2/1/17
to django...@googlegroups.com
Hi Matthew

I think I might not have explained my issue fully.
Within my jobs models.py I have the following:
def get_absolute_url(self):
        kwargs = {
                  'job_id': self.job_id,
                  'slug': self.slug,
                       }
        return reverse('job_detail', kwargs=kwargs)

I have the following urls patterns:
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'),

At this stage everything works fine so that clicking on a job in the job list takes you through to the detail for that job.
I have then created an application form for the jobs as follows:
def job_application(request, job_id, slug):
    # Retrieve job by id
    job = get_object_or_404(Job, job_id=job_id)
    sent = False

    if request.method == 'POST':
        # Form was submitted
        form = JobForm(request.POST)
        if form.is_valid():
            # Form fields passed validation
            cd = form.cleaned_data
            job_url = request.build_absolute_uri(job.get_absolute_url())
            subject = '{} ({}) Application for  "{}"'.format(cd['name'], cd['email'], job.title)
            message = 'Application for "{}" at {}\n\n{}\'s comments: {}'.format(job.title,  job_url, cd['name'], cd['comments'])
            from_email = ''
            to_list = ['']
            send_mail(subject, message, from_email, to_list)
            sent = True
    else:
        form = JobForm()
    return render(request, 'jobs/job_application.html', {'job': job, 'form': form, 'sent': sent})

The url for this form is:
url(r'^(?P<job_id>[-\w]*)/(?P<slug>[-\w]*)/job_application/$', views.job_application, name='job_application'),

I can go to this link directly in my browser as follows:
What I am looking to achieve is a link within the job detail page for that specific job that takes you through to the application fom for that job.

Any advice gratefully appreciated.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

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

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

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

Matthew Pava

unread,
Feb 1, 2017, 2:05:46 PM2/1/17
to django...@googlegroups.com

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.

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

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

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


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.

David Turner

unread,
Feb 2, 2017, 9:14:56 AM2/2/17
to django...@googlegroups.com
Hi Matthew
I understand what you are saying but I think the issue rests with my view as the link does not resolve but effectively creates the following:
  /jobs/1/slug-name/%E2%80%9D/jobs/1/slug-name/job_application/%E2%80%9D

Best

On 1 February 2017 at 19:04, Matthew Pava <Matthe...@iss.com> wrote:

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.

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

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

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


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.

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

David Turner

unread,
Feb 2, 2017, 10:05:06 AM2/2/17
to django...@googlegroups.com
Hi Matthew
Yes the error was in the view.

Many thanks for all you help.

Melvyn Sopacua

unread,
Feb 3, 2017, 11:07:13 AM2/3/17
to django...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages