eZ_Harry
eZ_Harry

Reputation: 826

Django - How to give a URL a Variable

I am in the process of making a edit listing system for my website and have run into an issue, the problem I am having is with my edit listing portal which displays all the listings associated with the relevant account. The idea is that people can click on any of the listings and it will take them to the edit listing form, however my edit listing form is suppose to be given a public key when you click on it. My problem is I dont know how to put the listings pk into the url. Could somebody please advise me on what to do?

Thanks

Code -

Listings Portal -

{% extends "base.html" %}

{% block content %}

    <h1 class="pageheader">Edit Your Job Listings</h1>

    <div class="joblistings">
    <p class="jobcounter">There are <b>{{ joblistings.count }}</b> jobs available</p>
    {% if joblistings %}
            {% for joblisting in joblistings %}
               {% if joblisting.active_listing %}
                 <div class="listings-item">
                   <a href="{% url 'editlisting' %}"> <--- THIS IS THE URL
                   <ul>
                   <li class="listing-title">{{ joblisting.job_title }} - {{ joblisting.business_name }}</li>
                   <li>Region: {{ joblisting.business_address_suburb }}</li>
                   <li>Pay Rate: ${{ joblisting.pay_rate }}</li>
                   <li>Contact Method: {{ joblisting.contact_method }}</li>
                   </ul>
                   </a>
                 </div>
               {% endif %}
            {% endfor %}

    {% else %}
        <p>Unfortunately all of the job opportunities have been taken at this moment.</p>
    {% endif %}
    </div>
{% endblock %}

Edit Listing View -

# This is the view which manages the edit listing page
@login_required(redirect_field_name='login')
def editlisting(request, pk):

    post = JobListing.objects.get(pk=pk)

    #if str(request.user) != str(post.user):
     #   return redirect("index")

    if request.method == "POST":
        print("test")
        form = JobListingForm(request.POST, instance=post, force_update=True)

        if form.is_valid():
            form.save()
            return redirect('index')

    else:
        print("else")
        form = JobListingForm(instance=post)

    context = {
        "form": form
    }

    return render(request, "editlisting.html", context)

Upvotes: 1

Views: 52

Answers (1)

Leistungsabfall
Leistungsabfall

Reputation: 6488

How about this?

<a href="{% url 'editlisting' joblisting.pk %}">Click me</a>

Upvotes: 5

Related Questions