checkbox returns empty list django templates - django

I'm fairly new to django.What I am trying to do is values from selected checkboxes from my django template. But it gives an empty list in views.py. I have searched for solutions but so far I have been unsuccessfull.
my template:
{% for x in project_list %}
<tr >
<td >
<label>
<input type="checkbox" name="checks[]" value="{{x.id}}"/>
<span><a href= '{% url "projects:ProjectDetail" Pid=x.id %}' >{{x.project_name}}</a><br></span>
</label>
</td>
</tr>
{%endfor%}
<a class="waves-effect waves-light btn-small" id="nxtBtn" href="{% url 'projects:ProjectDelete' %}">Delete selected projects</a>
views.py
def Delete_project(request,*args, **kwargs):
some_var = request.POST.getlist('checks[]')
print("printing project Id's")
print(some_var)
what I want to do is when I click on Delete selected projects I should get list of seleted checkbox values.But I'm getting an empty list when I print list in views.
Thanks in advance!

You don't seem to have a form. A link will not submit any data anywhere. You need an actual HTML form with an action attribute, and a submit button:
<form action="{% url 'projects:ProjectDelete' %}" method="POST">
{% for x in project_list %}
...
{% endfor %}
<button type="submit" class="waves-effect waves-light btn-small" id="nxtBtn">Delete selected projects</button>
</form>

Related

How to create a django form field with html

at the moment the html page looks something like this:
<form action="{% url 'sequence_details' %}" method="post">
{% csrf_token %}
<div class="container pb-3">
<table class="table">
{{ form.as_table }}
<tr><th><input type="text" id="extrafield"></th></tr>
</table>
</div>
<div class="container">
<input type="submit" class="btn btn-primary" value="Download">
</div>
</form>
extrafield appears as a text field but any text entered into it does not get sent to the backend, i.e. the result of form.cleaned_data.get("extrafield") is always None, no matter what was entered. I would like to be able to customise extrafield and write my own html for it, which is why I'm not just adding it to the form object. How can I get the result of extrafield's input to show up in form.cleaned_data?
you can get that by this in your .views function:
extrafield = request.POST.get('extrafield')
before that you need to give a name to your input:
<tr><th><input type="text" name="extrafield" id="extrafield"></th></tr>

Django search bar not getting to the right page

My page has two ways to search services - one is a search bar and the other is clickable categories. The clickable category go to a page that has a category primary key attached to the view and the search bar results do not have a category pk attached. Thus, I created two separate views for this reason. The problem is that both views go to the same page the index.html. I want the search bar to go to the index_search.html. I think I set-up everything correctly, but it still does not work.
Two views: search view, clickable items view:
#search form view
def service_finder(request):
model = Service
if ('s' in request.GET) and request.GET['s'].strip():
query_string = request.GET['s']
#get query is just searching for search terms in those fields of the query set
entry_query = get_query(query_string,['description', 'tags', 'category'])
table = Service.objects.filter(entry_query)[:60]
return render(request, 'index_search.html', {'table': table})
#clickable items view
def servicesListView(request, category):
model = Service
table = Service.objects.filter(category=category)[:60]
return render(request, 'index.html', {'table': table})
Here is the html template with both the search bar and clickable items:
{% block content %}
<main class="category">
{% include "search_form.html" %}
<section class="category-list">
<div class="container">
<div class="row">
{% for service in service_cat %}
<div class="col-sm-12 col-lg-6">
<a
href="{% url 'services_list' service.category %}"
{{ service.category }}
</a>
</div>
{% endfor %}
</div>
</div>
</section>
</main>
{% endblock %}
Here is the search bar file or search_form.html:
<div class="search-bar">
<div class="container">
<form method="get" action="{% url 'service_finder' %}">
{% csrf_token %}
<div class="input-group">
<input name='s' value="{{request.GET.s}}" type="text" class="form-control" placeholder="Search services" aria-label="Search" aria-describedby="search">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit" id="button-addon2">Go</button>
</div>
</div>
</form>
</div>
</div>
Here is the urls for the two pages:
path('services/<category>/', views.servicesListView, name='services_list'),
path('services/find/', views.service_finder, name='service_finder'),
SOLVED:
Fixed this by switching the order of the urls.
It's because the path /services/find/ matches the previous pattern /services/<category>/. Just explaining the reason why the fix worked!

Call two different urls from a form or input submit button Django

I have a form in which I have a table.In table I have a record(title).Now along with title I have two buttons delete and edit.Now I want is that, if I click edit it calls separate url along with primary key going through it or if I click delete It calls separate url with primary key going through.
here's my code:
<form method="POST" action="{% url 'essay:delete_view' teacher.id %}">
<div class="page-header">
<div class="span4">
<h1>Manage Essays</h1>
</div>
</div>
<table>
<tr>
<th>Title</th>
<th>Edit</th>
<th>Delete</th>
</tr>
{% csrf_token %}
{% for uploadassignment in teacher.uploadassignment_set.all %}
<tr>
<th for="uploadassignment{{ forloop.counter }}">{{uploadassignment.id}}</th>
<th><input type="submit" id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
<th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment" class="btn btn-primary"/></th>
</tr>
{% endfor %}
</table>
</form>
I want this: one url is called by below (Edit button) with teacher.id
<th><input type="submit" id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
And another url is called when hit delete button with teacher id.
<th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment" class="btn btn-primary"/></th>
So far due to action ="{% url 'essay:delete_view' teacher.id %}" in form I can switch to only one url.Kindly help in my code so I can switch to different urls with teacher id when clicked on delete or edit button.
urls.py:
url(r'^$', views.Indexview.as_view(), name='index'),
url(r'^login/$',views.loginform.as_view(),name='login'),
url(r'^addfile/$',views.Addfile.as_view(),name='file'), #essay/addfile
url(r'^addfile/$',views.EditEssay.as_view(),name='eEssay'),
url(r'^text/$',views.writetext.as_view(),name='text'),
url(r'^about/$',views.aboutus.as_view(),name='about'),
url(r'^stdprofile/$',views.studentprofile.as_view(),name='stdprofile'),
url(r'^logout/$', LogoutView.as_view(), name='user_logout'),
url(r'^(?P<teacher_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view'),

django: fill checkbox based on url params

i am having a form with only checkboxes:
<div class="container">
<form method="GET" class="form-inline" action="">
<div>
{% for temp in instance.menu_positions_tags.all %}
<label class="checkbox-inline">
<input type="checkbox" name="tags[]" value="{{ temp.name}}">{{ temp.name }}
</label>
{% endfor %}
</div>
<div style="margin-top:10px">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
When i select few checkboxes and submit the url changes to localhost/test/?tags[]=day4&tags[]=day2. After the page loads the all checkboxes are unselected.
Now, how to, in my form make day4 and day2 checkboxes checked.
If you have checkboxes in your form file you can just render it from the form when you receive the GET request. And I think your form type should be POST.

Django search bar problems

I'm trying to implement a search bar to my website, it's not working and I can't seem to figure out why. The search form looks like this:
<form class="navbar-form navbar-left" role="search" method="GET" action="{% url 'search' %}">
<div class="form-group">
<input type="text" class="form-control" name="q" value="{{request.GET.q}}">
</div>
<button type="submit" class="btn">Search</button>
</form>
url.py:
url(r'^search/$', views.SearchView, name='search'),
views.py:
def SearchView(request):
products = Photos.objects.all()
query = request.GET.get("q")
if query:
products = products.filter( Q(category__contains=query) ).distinct()
return render(request, 'urcle/search.html', {'products': products})
else:
return render(request, 'urcle/search.html', {'products': products})
search.html:
{% extends 'urcle/base.html'%}
{%block content%}
{% if products %}
<div id="inner" >
{% for item in products %}
<div id = 'content' align="center" ></div>
<div class="col-sm-4 col-lg-2" style="width:30%" >
<div class="thumbnail" align="left" >
<a href="{{item.name}}">
<img src="http://ec2-54-194-123-114.eu-west-1.compute.amazonaws.com/images/{{ item.name }}.jpg" class="img-responsive">
</a>
<div class="caption" >
{{item.title}} </li>
<br/>
</div>
</div>
</div>
{% cycle '' '' '' '' '' '<div class="clearfix visible-lg"> </div>' %}
{% endfor %}
</div>
{% else %}
<h1>nothing to see here!!!</h1>
{% endif %}
{%endblock%}
What's really bugging me is that all that gets displayed is the part that gets extended from base.html not even the tag on the else statement gets displayed, anyone know whats up?
The problem was a conflicting URL, which was taking me to the wrong view function