Ajax With Django Not Reload Page But Loop will reload - django

<div class="container" id="cd">
<div class="row">
<div class="col-2">
<label for="serialno" class="h4 text-center mx-4"> S.No </label>
</div>
<div class="col-3">
<label for="billno" class="h4 text-center"> Bill No</label>
</div>
<div class="col-5">
<label for="Items" class="h4 text-center"> Items</label>
</div>
<div class="col-2">
<label for="total" class="h4 text-center"> Total</label>
</div>
<hr>
</div>
{% for b,d in history.items %}
<div class="row" id="refresh">
<div class="col-2 py-2">
<label class="h6">{{forloop.counter}}. | {{b.created_at}}</label>
</div>
<div class="col-3 py-2">
<label class="h5">{{b}}</label>
</div>
<div class="col-5 py-2">
{% for i in d %}
<label class="h6">{{i.itemname}} x {{i.qty}} {{i.subtotal}}</label><br>
{% endfor %}
</div>
<div class="col-2 py-2">
<span class="h6">₹</span>
<label class="h6">{{b.grandtotal}}</label>
</div>
</div>
{% endfor %}
</div>
<script>
$(document).on('change','#myform',function(e){
e.preventDefault();
$.ajax({
type:'POST',
url:"{% url 'history' %}",
data:{
tb1:$('#cal').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
},
success:function(response){
},
});
});
</script>
How Can I reload this {% for b,d in history.items() %} without refresh a page using ajax
Any way to change history value using ajax Important Note value processed by onchange event
I am trying to reload the loop without a page reload
I want to change the history.items() django variable value after the ajax call and corressponding values can be iterated

{% for b,d in history.items %}
<div class="row" id="refresh-main">
<div class="row" id="refresh-child">
<div class="col-2 py-2">
<label class="h6">{{forloop.counter}}. | {{b.created_at}}</label>
</div>
<div class="col-3 py-2">
<label class="h5">{{b}}</label>
</div>
<div class="col-5 py-2">
{% for i in d %}
<label class="h6">{{i.itemname}} x {{i.qty}} {{i.subtotal}}</label><br>
{% endfor %}
</div>
<div class="col-2 py-2">
<span class="h6">₹</span>
<label class="h6">{{b.grandtotal}}</label>
</div>
</div>
</div>
{% endfor %}
<script>
$(document).on('change','#myform',function(e){
e.preventDefault();
$.ajax({
type:'POST',
url:"{% url 'history' %}",
data:{
tb1:$('#cal').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
},
success:function(response){
var pageURL = $(location).attr("href");
$('#refresh-main').load(pageURL + ' #refresh-child');
},
});
});
</script>

Related

IntegrityError in database stored vlaues in django?

'''
My problem is database values can't stored properly
In below I uploded views functions file,model file as an images. so in model there are three fields but database stored only image stored and other vlaues strored by default get null values so can you help to solving my problem..
'''
userproblem.html
<form method="POST" action="/uploadoc/" enctype="multipart/form-data" data-toggle="validator">
{% csrf_token %}
<div id="progressbarwizard">
<ul class="nav nav-pills bg-light nav-justified form-wizard-header mb-3">
<li class="nav-item">
<a href="#account-2" data-toggle="tab" class="nav-link rounded-0 pt-2 pb-2">
<i class="mdi mdi-account-circle mr-1"></i>
<span class="d-none d-sm-inline">Query Form</span>
</a>
</li>
</ul>
<div class="tab-content b-0 mb-0">
<div class="tab-pane" id="account-2">
<div class="row">
<div class="col-md-5">
<label class="col-md-12 col-form-label">Query Types:
<span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="text" class="form-control" data-validate="true" name="queryn" placeholder="Enter Query Types">
</div>
</div>
<div class="col-md-5">
<label class="col-md-12 col-form-label">Description of Query:
<span class="text-danger">*</span></label>
<div class="col-md-12">
<textarea type="text" class="form-control" data-validate="true" name="querydec" placeholder="Description of Query"></textarea>
</div>
</div>
</div><!-- /.First row end --><br>
<div class="row">
<div class="col-md-5">
<label for="Emailadrress" class="col-md-12 col-form-label">Upload Your File:
<span class="text-danger"></span></label>
<div class="col-md-12">
<div class="input-group-append">
<input type="file" id="img" name="img" accept="image/*">
</div>
</div>
</div>
<div class="col-md-6">
<label for="Emailadrress" class="col-md-12 col-form-label">Solutions:
<span class="text-danger"></span></label>
<div class="col-md-12">
<div class="input-group-append">
<textarea type="text" name="querysol" placeholder="Solutions of Query"></textarea>
</div>
</div>
</div>
</div> <!-- /.second row end -->
</div><br>
<div class="row">
<div class="col-12">
<div class="text-center">
<div class="mb-3">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck3">
<button type="submit" class="btn btn-primary">
<i class="icon-user icon-white"></i> Submit
</button>
</div>
</div>
</div>
</div> <!-- end col -->
</div>
</div> </div>
</form>
'[This is error and views function file my model imagespage][1]
You are trying to set c variable from userProblemModel named POSTed data in your uploadoc view.
c = request.POST.get('userProblemModel')
But in your form in html side there is no userProblemModel named input tag. That is why you are getting None value in c variable.

How to list the id with getlist method in Django checkbox

html file
Here I list the client and Process in the input tag
And select the and submit
{% extends 'client_pannel.html' %}
{% block content %}
<script>
function postConfirm() {
if (confirm('Are you sure you want to delete this client')) {
yourformelement.submit();
} else {
return false;
}
}
</script>
<form method="POST">
{% csrf_token %}
<div class="breadcome-area">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="breadcome-list single-page-breadcome">
<div class="row">
<div class="col-lg-10 col-md-6 col-sm-6 col-xs-12">
<div class="breadcome-heading">
<form role="search" class="sr-input-func">
<label>{{ emp.first_name }} {{ emp.last_name }}</label>
</form>
</div>
</div>
<div class="col-lg-2 col-md-6 col-sm-6 col-xs-12">
<a href="/employee_list/"> <button type="submit"
class="btn btn-info add-new">Submit</button></a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="data-table-area mg-b-15">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="sparkline13-list">
<div class="sparkline13-graph">
<div class="datatable-dashv1-list custom-datatable-overright">
<table id="table" data-toggle="table" data-pagination="true" data-search="true"
data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true"
data-key-events="true" data-show-toggle="true" data-resizable="true"
data-cookie="true" data-cookie-id-table="saveId" data-show-export="true"
data-click-to-select="true" data-toolbar="#toolbar">
<thead>
<tr>
<th>Process</th>
</tr>
</thead>
<tbody>
{% for client in form %}
<tr>
<td>
<input type="checkbox" name="cname[]" value="{{ client.id }}">
{{ client.Name }}
</td>
<td>
{% for process in client.clients %}
<input type="checkbox" name="process[]" value="{{ process.id }}">
{{ process.process }}<br />
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
{% endblock %}
views file
The below code is get the selected in checkbox. The getlist method return nothing
if request.method == "POST":
cname = request.POST.getlist('cname[]')
print("cname", cname)
process = request.POST.getlist('process[]')
print("process", process)

Custom Form for create and update the blogposts give Page Not Found error

I've create a simple form to create and update the blogposts. After the creation of the update view happen something strange that I can't solve.
views.py
def createPost(request):
if request.method == "POST":
form = BlogPostForm(request.POST or None)
if form.is_valid():
new_post = form.save(commit=False)
new_post.slug_post = slugify(new_post.title)
new_post.save()
return redirect('post_list')
else:
form = BlogPostForm()
context = {
'form': form,
}
template = 'blog/editing/create_post.html'
return render(request, template, context)
def updatePost(request, slug_post=None):
update_post = get_object_or_404(BlogPost, slug_post=slug_post)
form = BlogPostForm(request.POST or None, instance=update_post)
if form.is_valid():
update_post = form.save(commit=False)
update_post.slug_post = slugify(update_post.title)
update_post.save()
return redirect('post_list')
context = {
'form': form,
}
template = 'blog/editing/create_post.html'
return render(request, template, context)
urls.py
urlpatterns = [
path("blog/", views.listPost, name='post_list'),
path("blog/<str:slug_post>/", views.singlePost, name='single_post'),
path("blog/create-post/", views.createPost, name='create_post'),
path("blog/<str:slug_post>/update-post/", views.updatePost, name='update_post'),
path("blog/<str:slug_post>/delete-post/", views.deletePost, name='delete_post'),
]
post_list.html
<div class="row">
{% for post in post_list %}
{% if forloop.first %}<div class="card-deck">{% endif %}
<div class="card mb-3 shadow" style="max-width: 540px;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="{{ post.header_image_link }}" class="card-img" alt="{{ post.title }}" style="height: 250px;">
</div>
<div class="col-md-8">
<div class="card-body">
<h4 class="card-title">{{ post.title }}</h4>
<p class="card-text">{{ post.description }}</p>
<p class="card-text my-0 py-0"><small class="text-muted"><strong>Published: </strong>{{ post.publishing_date|date }}</small></p>
</div>
</div>
</div>
</div>
{% if forloop.counter|divisibleby:"4" or forloop.last %}</div>{% endif %}
{% if forloop.counter|divisibleby:"4" and not forloop.last %}<div class="card-deck">{% endif %}
{% empty %}
<div class="row justify-content-md-center my-3 mx-1 py-2 shadow bg-danger rounded">
<div class="col-md-auto">
<h1 class="text-center px-2 py-2" id="text-shadow">Empty list!</h1>
</div>
</div>
{% endfor %}
</div>
create_post.html
<form class="" method="POST" enctype="multipart/form-data" novalidate>{% csrf_token %}
<div class="form-group">
<div class="row">
<div class="col-sm-9">
<div class="form-group mb-4">
<div>{{ form.title }}</div>
<label for="id_title">
<span class="text-info" data-toggle="tooltip" title="{{ form.title.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.title.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div>{{ form.description }}</div>
<label for="id_description">
<span class="text-info" data-toggle="tooltip" data-placement="bottom" title="{{ form.description.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.description.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div>{{ form.contents }}</div>
<label for="id_contents">
<span class="text-info" data-toggle="tooltip" data-placement="bottom" title="{{ form.contents.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.contents.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div>{{ form.header_image_link }}</div>
<label for="id_highlighted">
<span class="text-info" data-toggle="tooltip" title="{{ form.header_image_link.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.header_image_link.errors }}</small>
</label>
</div>
</div>
<div class="col-sm-3">
<div class="form-inline mb-4 py-0">
<div class="input-group mx-1">
<label for="id_highlighted">{{ form.highlighted.label }}</label>
<div class="ml-1">{{ form.highlighted }}</div>
</div>
<div class="input-group mx-1">
<label for="id_draft">{{ form.draft.label }}</label>
<div class="ml-1">{{ form.draft }}</div>
</div>
</div>
<div class="form-group mb-4">
<label for="publishing_date_field">
{{ form.publishing_date.label }}
<small class="text-danger">{{ form.publishing_date.errors }}</small>
</label>
<div class="input-group date" data-target-input="nearest">
{{ form.publishing_date }}
<div class="input-group-append" data-target="#publishing_date_field" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
<script>
$(function () {
$("#publishing_date_field").datetimepicker({
format: 'DD/MM/YYYY HH:mm',
});
});
</script>
</div>
<div class="form-group mb-4">
<div class="row justify-content-md-center py-4 border border-warning rounded">
<h5 class="text-justify px-3">Clicca sul tasto che segue per vedere l'elenco delle immagini. Potrai copiare il link all'immagine che ti interessa ed usarlo per creare l'immagine di testata o migliorare i contenuti del post.</h5>
<button type="button" class="btn btn-primary mt-2 mx-2" data-toggle="modal" data-target=".bd-example-modal-xl">Image list</button>
</div>
<div class="modal fade bd-example-modal-xl" tabindex="-1" role="dialog" aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-body">
{% for image in images_url_list %}
{% if forloop.first %}<div class="card-deck">{% endif %}
<div class="card mx-1 my-1" style="height: 100%; width: 300px;">
<img src="{{ image.file.url }}" class="card-img-top h-100" alt="{{ image.file.url }}" id="{{ image.id }}">
<div class="card-body">
<button class="btn btn-outline-primary btn-sm" type="button" onclick="CopyToClipboard('{{ image.id }}')">Copy URL</button>
</div>
</div>
{% if forloop.counter|divisibleby:"3" or forloop.last %}</div>{% endif %}
{% if forloop.counter|divisibleby:"3" and not forloop.last %}<div class="card-deck">{% endif %}
{% endfor %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr>
<div class="row justify-content-md-center">
<div class="col-md-auto">
<input type="submit" class="btn btn-info" value="Pubblica">
</div>
</div>
</div>
</form>
I have this error message if I try to create a new post with my form:
Page not found (404) Request Method: GET Request URL:
http://127.0.0.1:8000/blog/create-post/ Raised by:
blog.views.singlePost
As I said this happen after the creation of updatePost. If I comment the path of the single post path("blog/<str:slug_post>/", views.singlePost, name='single_post'), I can create a post whitout problems.
What I've wrong?
Move path("blog/<str:slug_post>/", views.singlePost, name='single_post') to the bottom of your list, below the update and delete paths.
urlpatterns = [
path("blog/", views.listPost, name='post_list'),
path("blog/create-post/", views.createPost, name='create_post'),
path("blog/<str:slug_post>/update-post/", views.updatePost, name='update_post'),
path("blog/<str:slug_post>/delete-post/", views.deletePost, name='delete_post'),
path("blog/<str:slug_post>/", views.singlePost, name='single_post'),
]

Django - django-select2 doesn t work with inlineformset

I`ve made a inlineformset which contains a django-select2 field. The problem appears when I try to add another inline .The select2 data don't work on cloned field.
This is my template:
{{ form_m.management_form }}
{% for m in form_m.forms %}
<div class="{% cycle 'row1' 'row2' %} formset_row">
<div class="row">
<div class="col-sm-12">
{{ m.id }}
{{ m.medicament }}
</div>
</div>
<div class="row">
<div class="col-md-2">
{{ m.cantitate }}
</div>
<div class="col-md-2">
{{ m.nr_zile }}
</div>
<div class="col-md-2">
{{ m.mod_administrare }}
</div>
<div class="col-md-2">
{{ m.schema }}
</div>
</div>
</div>
{% endfor %}
This is my script for clone fields:
{% load static %}
<script src="{% static 'style/jquery.formset.js' %}"></script>
<script type="text/javascript">
$('.formset_row').formset({
addText: '<button id="add_more" class="btn btn-sm btn-success float-right mr-1">Adauga medicamentatie</button>',
deleteText: '<button class="btn btn-sm btn-danger float-right mr-5">Elimina</button>',
prefix: 'form_m_set',
});
</script>

Django eternicode bootstrap-datepicker doesn't work on phones

In my Django app i'm using bootstrap-datepicker. User chooses a date, then date is passed in Ajax post request to /check/ view, then sub_template is reloaded with retrieved data.
Everything works like a charm on desktop, but it seems like datepicker doesn't work on phones at all. I've tested it on Iphone 6s, android phone and windows phone(all default browsers). When i press date it just doesn't respond in any way.
Here is my main page html:
<div class="container">
<div class="row text-center">
<h2>Availability</h2>
</div>
<hr>
<div class="row text-center hidden-xs ">
</div>
<div class="row row-pad-30 text-center">
<div class="col-lg-1 md-hidden"></div>
<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12">
<h3>1. Choose date</h3>
<form style="color: white;" class="form-date" role="form" action="" method="get">
{% csrf_token %}
<div class="form-group" id="datepicker">
<div></div>
<input data-date-format="YYYY-MM-DD" type="hidden" name="dt_due" id="dt_due" val="">
</div>
</form>
</div>
<div id="refresh-div" class="timedisplay">{% include 'booking/time.html' %}</div>
<div class="col-lg-1 md-hidden"></div>
</div>
</div>
sub page html:
<div class = "col-lg-6">
<h3 class = "h3-hour">2. Choose hour</h3>
<div class="row row-pad-20">
{% for time_list in availability_table %}
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
{% for i in time_list %}
<button type="submit" class="fade-in-quick btn btn-lg btn-block btn-info btn-core" value="{{ forloop.counter0 }}" name="time" id="time">{{ i.0|date:'H:i' }} do {{ i.1|date:'H:i' }}</button>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
main page script:
$('#datepicker div').datepicker({
language:"pl",
startDate: "+1d"
}).on('changeDate', function(event) {
var date = event.format()
console.log(date);
$.ajax({
type:'POST',
url: '/check/',
data: {'date': date, 'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val()},
success: function(data) {
$('#refresh-div').html(data);
}
});
});
Any idea why it doesn't work?
So apparently this div:
<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12"></div>
Was causing problems. When i just deleted all classes after col-lg-4(they are not necessary anyway) everything is working correctly.