GET表单<button>不重定向,<a> not passing kwarg

时间:2017-03-18 20:24:23

标签: python django jinja2

If I use a button in the get form it doesn't redirect to the search page using the kwarg, if I use a <a> it redirects but passes an empty object.

Trying to write a function that filters my model objects based on a parameter set through a form.

User selects a project engineer and only sees projects that they're assigned to.

I'm somewhat new to django. Just trying to make a user controlled filter... I'm not getting any error messages.

This is my template

{% extends 'projects/base.html' %}

{% block content %}

<div>
    <form method="GET">
    <button href="{% url 'project_search' project_engineer=project_engineer %}" class="save btn btn-default">Search by Project Engineer</button>
    <select name="project_engineer">

    {% for pe in pe_list %}
            <option value="{{ pe }}">{{ pe }}</option>
    {% endfor %}
    </select>

    </form>
</div>
    {% for post in posts %}
        <div>               
            <h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.name }}</a></h1>
            <p>{{ post.description|linebreaksbr }}</p>
        <p>Project Engineer: {{ post.project_engineer|linebreaksbr }}</p>
        <p>Last Modified: {{ post.published_date }}</p>
        </div>
    {% endfor %}
{% endblock %}

This is my views

def project_list(request):
    posts = Project.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    pe_list=["all"]
    for x in range(0, len(posts)):
        if posts[x].project_engineer not in pe_list:
             pe_list.append(posts[x].project_engineer)
    return render(request, 'projects/project_list.html', {'posts': posts, 'pe_list': pe_list})

def project_search(request, project_engineer):
    variable_column = 'project_engineer'
    search_type = 'contains'
    filter = variable_column + '__' + search_type
    posts_all = Project.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    posts = Project.objects.filter(**{ filter: project_engineer }).order_by('published_date')
    pe_list=["all"]
    for x in range(0, len(posts_all)):
        if posts_all[x].project_engineer not in pe_list:
             pe_list.append(posts_all[x].project_engineer)
    return render(request, 'projects/project_list.html', {'posts': posts, 'pe_list': pe_list})

urls.py

url(r'^(?P<project_engineer>.*)/$', views.project_search, name='project_search'),

0 个答案:

没有答案