Django - 如何为URL提供变量

时间:2016-01-14 12:20:27

标签: python django

我正在为我的网站制作编辑列表系统并遇到问题,我遇到的问题是我的编辑列表门户,它显示了与相关帐户相关的所有列表。我们的想法是,人们可以点击任何列表,然后将它们带到编辑列表表单,但是当我点击它时,我的编辑列表表格会被假定为公钥。我的问题是我不知道如何将列表pk放入网址。有人可以告诉我该做什么吗?

由于

代码 -

列表门户 -

{% 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 %}

编辑列表视图 -

# 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)

1 个答案:

答案 0 :(得分:5)

这个怎么样?

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