I have a div class that get content from for loop queryset
<div class="form-group row">
<div class="col-md-4">Số hợp đồng</div>
<label class="col-md-8" id="so_hd_goc">{{hd.so_hd}}</label>
</div>
I want to use ajax to get the value from id="so_hd_goc" and send it to views:
my views:
def check_so_hd(request):
check_shd=request.GET.get("check_shd")
print("check_shd",check_shd)
return HttpResponse(check_shd)
my form:
<div class="card card-body">
<form method="POST" id = "save_bo_sung_sdt">
{% csrf_token %}
<div class="form-group row">
<div class="col-md-6">Người liên hệ</div>
<div class="col-md-6" id ="sdt_nguoi_lien_he" name="sdt_nguoi_lien_he">{{form1.nguoi_lien_he}}</div>
</div>
<div class="form-group row">
<div class="col-md-6">Số điện thoại</div>
<div class="col-md-6" id ="sdt_dien_thoai" name="sdt_dien_thoai">{{form1.dien_thoai}}</div>
</div>
<div class="form-group row">
<div class="card-footer" >
<button type="submit" class="btn btn-primary" id="save_phone" name="save_phone">Lưu số điện thoại</button>
</div>
</div>
</form>
</div>
my ajax:
<script>
const form1 = document.getElementById('save_bo_sung_sdt')
const sdtContainer = document.getElementById('sdt-body')
form1.addEventListener('submit', checkshd)
function checkshd(e){
e.preventDefault()
check_shd=document.getElementById(so_hd_goc)
$.ajax({
type: "GET",
url: "{% url 'check_so_hd' %}",
data:
{
check_shd:check_shd
},
success:function(data){
alert(data)
}
})
};
</script>
my url
path('check_so_hd', CallerViews.check_so_hd, name="check_so_hd"),
There is no error> just not print any result
Try this:
form1.addEventListener('click', (e) => {
e.preventDefault();
check_shd=document.getElementById('so_hd_goc').innerText;
console.log("check_shd=",check_shd);
$.ajax({
type: "GET",
url: "{% url 'check_so_hd' %}",
data:
{
'check_shd' : check_shd
},
success:function(data){
alert(data)
}
})
});
Related
<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>
how can i add a file in my front end vuejs and download my file from my backend django rest framework with v-model in vuejs
my modele is article ,
class Article(models.Model):
conference = models.ForeignKey(Conference,related_name='articles',on_delete=models.CASCADE)
title =models.CharField(max_length=50)
Auteur =models.CharField(max_length=50)
resume =models.TextField()
motcles =models.TextField()
create_by = models.ForeignKey(User, related_name='articles',on_delete=models.CASCADE)
fichier = models.FileField(upload_to='uploads',blank=True, null=True)
def __str__(self):
return self.title
this code is about how i add a new article , i should here add how to enter a file
<form v-on:submit.prevent="submitArticle()">
<div class="field">
<label class="label">Title</label>
<div class="control">
<input type="text" class="input" v-model="article.title">
</div>
</div>
<div class="field">
<label>Auteur</label>
<div class="control">
<input type="title" class="input" v-model="article.Auteur">
</div>
</div>
<div class="field">
<label class="label">Resume</label>
<div class="control">
<textarea type="text" class="input" v-model="article.resume"></textarea>
</div>
</div>
<div class="field">
<label>motcles</label>
<div class="control">
<input type="title" class="input" v-model="article.motcles">
</div>
</div>
<div id="app">
<a
href="#"
#click.prevent="
downloadItem({
url:
'https://test.cors.workers.dev/?https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
label: 'example.pdf',
})
"
>
download
</a>
</div>
<div class="field">
<div class="control">
<button class="button is-link">submit</button>
</div>
</div>
</form>
this is how i enter my article
submitArticle() {
console.log('submitArticle')
const conferenceID = this.$route.params.id
this.errors = []
if (this.article.title === '') {
this.errors.push('The title must be filled out')
}
if (this.article.resume === '') {
this.errors.push('The content must be filled out')
}
if (!this.errors.length) {
axios
.post(`/api/v1/add-article/${conferenceID}/`, this.article)
.then(response => {
this.article.title = ''
this.article.Auteur = ''
this.article.resume = ''
this.article.motcles = ''
this.articles.push(response.data)
})
.catch(error => {
console.log(error)
})
please i need a help how to add and download a file ?
I have to set up the user should be able to view the contact info of customer, For that I'm using ajax. the code is working but the problem is the ajax is only working in the first item, other buttons are not working what is the reason
another issue is when i hit the button again it showing data agin and agin how to control it
how to solve these two issues
views.py
def contact_details(request):
property_details = Property.objects.all()
seraializer = PropertySerializer(property_details, many=True)
data = seraializer.data
return JsonResponse(data, safe=False )
HTML page(including ajax)
{% if accomodations %}
{% for accomodation in accomodations %}
<div class="row">
<div class="col-sm-2 col-md-2"></div>
<div class="col-sm-4 col-md-4">
<img src="{{accomodation.images.url}}" class="img-responsive" style="width:100%">
</div>
<div class="col-sm-5 col-md-5">
<div class="caption">
<h3>{{ accomodation.headline }}</h3>
<div>
{{ accomodation.city }}
</div>
<div>
{{ accomodation.location }}
</div>
<div>
{{ accomodation.facilites }}
</div>
<div>
{{ accomodation.rent }}
</div>
<div>
{{ accomodation.owner }}
</div>
<div>
{{ accomodation.id }}
</div>
<button id="request-btn" name="property_id" class="btn btn-primary">Contact info:</button>
</form>
<div id=response></div>
</div>
</div>
</div>
<hr>
{% endfor %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#request-btn").click(function(){
$.ajax({
url: "{% URL 'contact_details' %}" ,
type: "GET",
dataType:"json",
success:
function(result){
$.each(result, function() {
html = "Email:" +this.email + "<br>" +"Mobile:" + this.mobile +"<hr>"
$("#response").append(html)
});
}
});
});
});
</script>
{% else %}
<h1>Not Found</h1>
{% endif %}
The problem is that you are using an id instead of a class, that's why only the first button works. Change it to class instead, then it should work. Try this:
<script type="text/javascript">
$(document).ready(function(){
$(".request-btn").click(function(){
var reference = this;
$.ajax({
url: "{% URL 'contact_details' %}" ,
type: "GET",
dataType:"json",
success:
function(result){
$.each(result, function() {
html = "Email:" +this.email + "<br>" +"Mobile:" + this.mobile +"<hr>"
$("#response").append(html)
});
}
});
});
});
</script>
I'm not sure what your second question is - could you please rephrase it or add details?
<div class="content-section">
<form method="POST" id="moviesForm" ajax_load_cast="{% url 'ajax_load_cast' %}" novalidate enctype="multipart/form-data" >
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4"></legend>
<div class="form-group">
<label for="id_cast" class="col-form-label requiredField">Cast<span class="asteriskField">*</span> </label> <div class=""> <input type="text" name="cast" value="" maxlength="50" class="textinput textInput form-control" required="" id="id_cast">
<select id="add_cast2" multiple style="width:100%" ></select>
</div>
</div>
<div id="" class="form-group">
</div>
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Submit</button>
</div>
</form>
</div>
{% block js%}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
var ajaxResult=[];
$("#id_cast").keyup(function () {
var url = $("#moviesForm").attr("ajax_load_cast");
var text_input = $('#id_cast').val();
$.ajax({
url: url,
data: {
'text_input': text_input
},
success: function (data) {
$("#add_cast2").html(data);
}
});
});
</script>
{% endblock js%}
{% endblock content %}
I am implementing a django multiselect option formField with basic html and ajax request so that it filters results before showing all the results. But I don't know how to implement select and deselect with the ajax response that brings multiselect options from database. With each response it deselects the selected options.
There is the following form.
<form id="contact_form" class="get-in-touch-form" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h1 class="title {% if component.css_title_style %}{{ component.css_title_style }}{% endif %}">
{{ component.title }}
</h1>
<p class="subtitle {% if component.css_subtitle_style %}{{ component.css_subtitle_style }}{% endif %}">
{{ component.description }}
</p>
<div class="row">
<div class="col-sm-6">
<div class="input-wrapper">
{{ contact_form.name }}
</div>
<div class="input-wrapper">
{{ contact_form.email }}
</div>
<div class="input-wrapper">
{{ contact_form.phone }}
</div>
<div class="input-wrapper" style="position: relative;">
{{ contact_form.text }}
<img id="clip" src="{% static 'images/clip.png' %}" style="position: absolute; left: 95%; top: 5%;" onclick="document.querySelector('#id_file').click()">
</div>
<div class="input-wrapper" style="display: none">
{{ contact_form.file }}
</div>
<div class="input-wrapper">
<div class="col-md-5">
<div class="custom-checkbox">
{{ contact_form.nda }}
<label for="checkbox1">Send me an NDA</label>
</div>
</div>
<img src="/static/images/loader.svg" height="65" alt="Loading..." style="display: none;">
<input type="submit" value="Send" class="green-button">
</div>
</div>
<div class="col-sm-6">
<img src="{% static 'images/map_all.png' %}">
</div>
</div>
</form>
When I submit it and errors occur, text is displayed near the field that generates the error in its usual form. How can I style this text? For example, to highlight this field somehow?
I tried to do this through ajax, but it didn’t work. When I try to send a form containing errors to the server, I immediately get an error (for example, this field is required) and accordingly it does not reach any styling.
Now the error output looks like this.
How can you style it this way?
Add novalidate to form tag.
Add following block after your input fields.
<div class="contact_form_name" style="font-size:15pt; position: absolute; right: 30px; top: 20px; color: #FD7900; display: none"></div>
Add jquery + ajax code.
var frm = $('#contact_form');
frm.submit(function (e) {
e.preventDefault();
$.each($("div[class^='contact_form_']"), function(ind, value){
$(value).hide();
$(value).prev().css('border', 'none')
})
$.ajax({
type: frm.attr('method'),
url: '',
data: frm.serialize(),
success: function(data) {
console.log(data)
if (data.result == 'error'){
$.each(data.response, function(key, value) {
$('.contact_form_' + key).css('display', 'block')
$('.contact_form_' + key).prev().css("border", 'solid #FD7900')
$('.contact_form_' + key).text(value)
})
} else {
location.reload();
}
}
});
return false;
});
Add view function.
def post(self, request, *args, **kwargs):
contact_form = ContactFormForm(request.POST, request.FILES)
if contact_form.is_valid():
contact_form.save()
return JsonResponse({})
else:
response = {}
for k in contact_form.errors:
response[k] = contact_form.errors[k][0]
return JsonResponse({'response': response, 'result': 'error'})