Skip to content

Instantly share code, notes, and snippets.

@yadav-vikas
Created June 9, 2022 22:24
Show Gist options
  • Save yadav-vikas/ddb85290c431d03c85d573eaef911f91 to your computer and use it in GitHub Desktop.
Save yadav-vikas/ddb85290c431d03c85d573eaef911f91 to your computer and use it in GitHub Desktop.
from django.contrib import admin
from .models import Personal
admin.site.register(Personal)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Boolea</title>
</head>
<body>
<h1>
This is Boolean Form
</h1>
<div class="container py-5">
<h3>Contact</h3>
<form method="post" action="/oppo/{{user}}/contact/">
{% csrf_token %}
<!-- <div>
<input type="checkbox" id="scales" name="scales" value="{{ selected_data.name }}">
<label for="scales">Name</label>
</div>
<div>
<input type="checkbox" id="horns" name="horns" value="{{ selected_data.email }}">
<label for="horns">Email</label>
</div>
<div>
<input type="checkbox" id="horns" name="horns" value="{{ selected_data.phone_no }}">
<label for="horns">Phone No</label>
</div> -->
{{form}}
<button class="btn btn-primary"></a>Submit</button>
</form>
</div>
<!-- <form method="post">
{% csrf_token %}
<div style="display:inline-block">
<label>NAME</label>
<label class="switch">
<input type="checkbox" id="checkbox" value="{{ instance.name }}">
<span class="slider round"></span>
</label>
</div>
<div style="display:inline-block">
<label>EMAIL</label>
<label class="switch">
<input type="checkbox" id="checkbox" value="{{ instance.name }}">
<span class="slider round"></span>
</label>
</div>
<div style="display:inline-block">
<label>PHONE NO</label>
<label class="switch">
<input type="checkbox" id="checkbox" value="{{ instance.name }}">
<span class="slider round"></span>
</label>
</div>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#checkbox').change(function() {
$.post("/about/", {
id: '{{workexperiance.id}}',
isworking: this.checked,
csrfmiddlewaretoken: '{{ csrf_token }}'
});
});
});
</script> -->
</body>
</html>
from django import forms
from .models import Personal
class IdleForm(forms.ModelForm):
class Meta:
model = Personal
fields = ['name', 'email', 'phone_no',]
widgets = {
'name': forms.CheckboxInput(attrs={"class": "form-check-input", "id": "flexSwitchCheckChecked"}),
'email': forms.CheckboxInput(attrs={"class": "form-check-input", "id": "flexSwitchCheckChecked"}),
'phone_no': forms.CheckboxInput(attrs={"class": "form-check-input", "id": "flexSwitchCheckChecked"}),
}
from django.db import models
from django.contrib.auth.models import User
from blog.models import Post
from django.shortcuts import reverse
class Personal(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="personal_author")
name = models.BooleanField(blank=True, default=False)
email = models.BooleanField(blank=True, default=False)
phone_no = models.BooleanField(blank=True, default=False)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="personal_link_user_created", null=True, blank=True)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="personal_user_updated", null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True,null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True,null=True, blank=True)
def get_absolute_url(self):
return reverse("contact", kwargs={"id": self.id})
class Meta:
managed = True
def __init__(self, *args, temp=65, **kwargs):
self.temp = temp
return super().__init__(*args, **kwargs)
from django.urls import path
from . import views
urlpatterns = [
path("<str:username>/contact/", views.contact, name="contact"),
path("", views.about, name="oppo-about"),
]
from django.shortcuts import render, redirect
from .forms import ExampleForm,IdleForm, tableedit
from .models import Personal
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView
from django.http import HttpResponseRedirect
@login_required
def contact(request, username):
u = User.objects.get(username=username)
instance = get_object_or_404(Personal, author=u)
if request.method == 'POST':
form = IdleForm(request.POST or None, instance=instance)
if form.is_valid():
data = form.cleaned_data
if Personal.objects.filter(author=u).exists():
Personal.objects.filter(author=u).update(name=data['name'],email=data['email'],phone_no=data['phone_no'])
else:
Personal(author=u,name=data['name'],email=data['email'],phone_no=data['phone_no']).save()
form.save()
return render(request, "oppo/boolean.html", {'form':form, 'instance': instance, 'user':u})
else:
form = IdleForm()
print(instance.author)
print(instance.name)
print(instance.email)
print(instance.phone_no)
return render(request, "oppo/boolean.html", {'form':form, 'instance': instance, 'user':u})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment