Call two different urls from a form or input submit button Django - 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'),

Related

checkbox returns empty list django templates

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>

Getting error in url from HTML template to urls. No reverse match

I am trying to delete a record the on basis of primary key, so I am trying to pass a PK to url into a view function but getting this error:
django.urls.exceptions.NoReverseMatch:
Reverse for 'delete_view' with arguments '('',)' not found. 1 pattern(s) tried: ['essay/(?P<essay_id>[0-9]+)/delEssay/$']
My html 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.title}}</th>
<th><input type="submit" value="Edit" class="btn btn-primary"/></th>
<th><input type="submit" value="Delete" name="delEssay" id="delEssay" class="btn btn-primary"/></th>
</tr>
{% endfor %}
</table>
</form>
URL:
url(r'^(?P<essay_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view')
View Function:
def delete_view(request,essay_id):
print("Delete")
print(essay_id)
object = UploadAssignment.objects.get(id=essay_id)
object.delete()
return render(request, 'essay/THome.html')
You are trying to access the object ID teacher.id, instead you try with primary key pk like teacher.pk
I had the same problem and i replaced it with pk and it worked.
hope this solves your problem.

How to open a template as modal (detail user data) in user_list page (Django 1.11)

I have a user list page which works fine, and a user detail page which works fine too that I call from urls.py in seperate window. I want to open user detail in user list page in a modal window.
user_list.html
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr userid="{{user.id}}" class="edit_user">
<td>{{user.first_name}}</td>
<td>{{user.last_name }}</td>
<td>{{user.username }}</td>
<td>
<form class="right user_delete" method="POST" userid="{{user.id}}"
action="{% url 'user_delete' user.id%}">
{% csrf_token %}
<input class="btn btn-danger btn-sm" type="submit" value="DELETE">
</form>
</td>
<td><a type="button" class="btn btn-primary edit_user" href="{% url 'user_details' user.id %}" target="#edit_user"> UPDATE </a></td>
</tr>
{% empty %}
<tr>
<td>No Projects.</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#registerformmodal"> New user </button>
user_detail.html
<h1>User Details</h1>
<p>{{ user_details.username }}</p>
<p>{{ user_details.first_name }}</p>
views.py
def user_details(request, userid):
user = User.objects.get(id=userid)
template = loader.get_template('vts/user_detail.html')
context = {
'user_details': user,
}
return HttpResponse (template.render(context, request))
The button data-target here,
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#registerformmodal"> New user </button>
looks for #registerformmodal which is neither associated with <form> nor <div>
You can add the id to your form as this
<form id="#registerformmodal" class="right user_delete" method="POST" userid="{{user.id}}">
...
</form>

Django: create generic link from radio select

i have a index.html that display all databases i own. And now i wanna give some options to handle the database (e.g. edit, delete, ...). So i wanna create a radio select with the database-ids and some links for redirecting the right url.
My aim: click on the requested action brings the user to the right url.
I hope, that was understandable :)
Can anyone help me, please.
// edited
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
</tr>
{% endfor %}
</table>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</form>
I'd suggest you use jQuery or so to accomplish this.
However, you need to adjust your id's first, because an id/class can't start with a number in html, so rather use id="database_{{ db.id }}"
I don't know what {% show_task_link "./{{db.id}}/edit" _("Edit database") %} does, but make sure it has an id so you can access it through jQuery which would be something like:
<script type="text/javascript>
$(function(){
$('.db').click(function(){
if($(this).is(':checked')){
$('#id_for_your_link').html() // put your link in html, add the id through $(this).val()
}
}
})
</script>
Note: i haven't tested the jQuery
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
<td>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</td>
</tr>
{% endfor %}
</table>
</form>

Create template for forms django

I currently have a login.html, which has inside it a form for login. All my forms need to look similar, however will have different content. At the moment my code for it looks like this:
<form enctype="multipart/form-data" method="POST" action="">
<fieldset>
<legend>{{ legend }}</legend>
<div style="line-height:18px">{% block top %}{% endblock %}<br></div>
<table class="form">
{% for text,name,inputtype in formcontent %}
<tr>
<td class="right">
<label>{{ text }}:</label><br/>
</td><td style="float:left">
<input name="{{ name }}" type="{{inputtype}}">
</td>
</tr>
{% endfor %}
</table>
<input value="{{ button }}" type="submit">
<div style="line-height:18px">
<div class="err">
{{ err }}
</div>
{{ bottom }}
</div>
</fieldset></form>
I was wondering if there was a better way to do it than to put variables into my urls.py (given that there may be multiple forms on hte page, I don't believe that this would work under all circumstances). A nice solution would be to create a class, but I don't know how to call this class from a page. I know it would involve making a tag, but I don't see how you can provide a 2d array as an argument from a template.
Django Crispy Forms were made for this exact reason. I think they are exactly what you need.