Reputation: 563
I have a job site where a user enters a zip code into a form and a list of jobs matching that zip code is displayed search.html :
<h6>Results: {{ number_of_results }}</h6>
{% for job in jobs_matching_query %}
<h2><a class="job_link" href="#">{{ job.job_title}}</a></h2>
<p class="job_address lead">{{ job.establishment_name }} - {{ job.address }}</p>
{% endfor %}
<form action = "{% url 'email_subscription' %}">
<p>Subscribe to recieve job alerts near {{ query }}:</p> <!-- query stores zip code-->
<input type="text" name= "email" placeholder="Email">
<button class="btn" type="submit">Subscribe</button>
</form>
The search form is handled by the following view (not sure if I need to include this or not):
def job_query(request):
if request.method == "GET":
query = request.GET.get('query')
jobs_matching_query = Job.objects.filter(zip_code__iexact = query) | Job.objects.filter(city__iexact=query) | Job.objects.filter(state__iexact=query)
number_of_results = 0
for job in jobs_matching_query:
number_of_results = number_of_results + 1
return render(request, 'core/search.html', {'query': query ,'jobs_matching_query': jobs_matching_query, 'number_of_results': number_of_results})
On that search.html I have an email subscription box (subscribe to recieve alerts for that particular zip code), is there a way to pass the value of query
from that page to an email_subscription
view? I believe I've seen this done in the url so here is the url for said view
url(r'^email_subscription$', views.email_subscription, name="email_subscription"),
Upvotes: 2
Views: 239
Reputation: 2035
You can use django sesions for that you can save the query in django session
#put it somewhere
request.session['query'] = my_query
#then you can access it from the other view in views.email_subscription
it is going to work but im not sure if this is a practitacal one
Upvotes: 1