Tuesday, January 31, 2017

Re: Link to urls containing slug

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.

On 31 January 2017 at 16:32, Matthew Pava <Matthew.Pava@iss.com> wrote:

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!

 

From: 'David Turner' via Django users [mailto:django-users@googlegroups.com]
Sent: Tuesday, January 31, 2017 9:09 AM
To: django-users@googlegroups.com
Subject: Re: Link to urls containing slug

 

I had tried that but unfortunately it doesn't work but thanks anyway.

 

On 31/01/2017 14:57, Matthew Pava wrote:

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>

 

From: 'dtdave' via Django users [mailto:django-users@googlegroups.com]
Sent: Tuesday, January 31, 2017 8:46 AM
To: Django users
Subject: Link to urls containing slug

 

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

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

No comments:

Post a Comment