Django MultiSelect Dropdown Checkbox to update Models - python

How can I create a multiselect dropdown checkbox (checked=True, unchecked=False) to update my models?
Below is my current code:
models.py:
class TestProfile(models.Model):
revenue = models.BooleanField(default=False)
costOfRevenue = models.BooleanField(default=False)
forms.py:
class CreateTestProfile(forms.ModelForm):
class Meta:
model = TestProfile
fields = '__all__'
views.py:
def test(request):
profile_form = CreateTestProfile()
if request.method == 'POST':
profile_form = CreateTestProfile(request.POST)
if profile_form.is_valid():
profile_form.save()
return redirect('/dashboard')
else:
profile_form = CreateTestProfile()
return redirect('/dashboard')
dict = {'profile_form': profile_form}
return render(request, 'demo/test.html', dict)
test.html:
<div class="w-1/2">
<form method="POST">
{% csrf_token %}
{{profile_form|crispy}}
<input type="submit" value="Submit">
</form>
</div>
If the dropdown is checked for revenue, then I would like to update the TestProfile model's revenue value to be True.

Related

How can my django form grab current username and email and submit them into another database table?

In my django project I have a form VisitorRegForm which collects scanned_id and body_temp and submits data to my database table ClinicalData, since the current user is already logged in, how can I make this form to also be able grab current logged in user's username and email (Note: username and email were both created from the built-in django User model) and submit these four items: 'scanned_id', 'body_temp', 'username', 'email' into the database table ClinicalData?
Don't mind the missing details and inports, the form works and am able to submit 'scanned_id', 'body_temp'. Help me on the easiest way I will be able to submit these four items: 'scanned_id', 'body_temp', 'username', 'email' looking at the details shown in the files below:
Where should it be easier to achieve this; in the views.py or the template file?
forms.py
class VisitorRegForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
class ScannerForm(ModelForm):
class Meta:
model = ClinicalData
fields = ['scanned_id', 'body_temp']
models.py
class ClinicalData(models.Model):
...
scanned_id = models.CharField(verbose_name="Scanned ID", max_length=50, null=True)
body_temp = models.DecimalField(verbose_name="Body Temperature", max_digits=3, decimal_places=1, null=True)
username = models.CharField(verbose_name="Facility Name", max_length=200, null=True)
email = models.EmailField(verbose_name="Visitor Email", max_length=200, null=True)
...
views.py
Am assuming I might need like two variables here in the views that should help me like the ones i've commented out, if it should start from here, help me how it should succeed:
#login_required(login_url='login')
def scanEnter(request):
#grab_username = function_to_grab_current_logged_in_username_HELP_ME_HERE
#grab_email = function_to_grab_current_logged_in_user_email_HELP_ME_HERE
form = ScannerForm(request.POST)
if request.method == 'POST':
form = ScannerForm(request.POST)
if form.is_valid():
form.save()
return redirect('scanenter')
context = {'form': form}
return render(request, 'scan_enter_fac.html', context)
scan_enter_fac.html
<form action="" method="post">
{% csrf_token %}
<div class="">scanned_id<input type="text" id="multiple" class="form-control" placeholder="or Type ID here; e.g: ACCTS-147" name="scanned_id"></div>
<div class="">body_temp<input type="text" class="form-control" placeholder="e.g: 36.5" name="body_temp"></div>
<button type="submit" class="btn btn-danger btn-block">Submit</button>
</form>
views.py:
#login_required(login_url='login')
def scanEnter(request):
#grab_username = function_to_grab_current_logged_in_username_HELP_ME_HERE
#grab_email = function_to_grab_current_logged_in_user_email_HELP_ME_HERE
form = ScannerForm(request.POST)
if request.method == 'POST':
form = ScannerForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.username = request.user.username #new
form.email = request.user.email #new
form.save()
return redirect('scanenter')
context = {'form': form}
return render(request, 'scan_enter_fac.html', context)
scan_enter_fac.html
<form action="" method="post">
{% csrf_token %}
<div class="">{{ form.scanned_id }}</div>
<div class="">{{ form.body_temp }}</div>
<button type="submit" class="btn btn-danger btn-block">Submit</button>
</form>

How to display or render multiple forms in django

I'm currently working on a personal project where in a page (purchases page), there will be the main form (includes product name, product category and product specifications fields).
now, there is a link "Add New Product Category" that activates the modal. This modal includes a separate form.
What you are seeing is the final output or what I want the page to be, i only did that in html, no django coding involve.
My (stupid) question is, how am i going to display BOTH forms? I don't understand how {{form}} works.
I successfully rendered the productCategoryForm in the modal by using the code {{form}} but when I do the same in the second form productDetailsForm it's not rendering or displaying. It's blank. I'm not sure how is this going to work.
Below is my Views.py and Forms.py codes.
Views.py
def addNewProduct(response):
c_form = productCategoryForm(response.POST)
p_form = productDetailsForm(response.POST)
if response.method == "POST":
if c_form.is_valid():
a = c_form.cleaned_data["productCategField"]
b = productCategoryModel(productcategoryCol=a)
b.save()
return HttpResponseRedirect("/acctg/purchases/")
if p_form.is_valid():
c = p_form.cleaned_data["productNameField"]
d = productModel(productnameCol=c)
d.save()
return HttpResponseRedirect("/acctg/purchases/")
context = {
"p_form": p_form,
"c_form": c_form
}
return render(response, 'purchases.html', context)
Forms.py
class productCategoryForm(forms.Form):
productCategField = forms.CharField(label="Product Category", max_length=100, widget= forms.TextInput(attrs={'class':'form-control col-sm-8 col-form-label'}))
class productDetailsForm(forms.Form):
productNameField = forms.CharField(label="Product Name", max_length=100, required=True, widget=forms.TextInput(attrs={'placeholder':'Enter Product Name', 'class':'form-control col-sm-8 col-form-label'}))
Models.py
# Create your models here.
class productCategoryModel(models.Model):
productcategoryCol = models.TextField()
def __str__(self):
return self.productcategoryCol
class productModel(models.Model):
productnameCol = models.TextField()
productspecsCol = models.TextField()
#productcategCol = models.ForeignKey(productcategoryCol, default=None, on_delete=models.CASCADE)
def __str__(self):
return self.productnameCol
Appreciate your help on this. Thank you!
You can pass both forms in your template through context.
def addNewProduct(request):
p_form = productDetailsForm()
c_form = productCategoryForm()
if request.method == "POST":
p_form=productDetailsForm(request.POST)
if p_form.is_valid():
p_form.save()
return redirect("/")
context = {
"p_form": p_form,
"c_form": c_form
}
return render(response, 'purchases.html', context)
Now in the template you can render both forms with {{p_form}} and
{{c_form}}. And provide different actions and different views for both forms.
EDIT:
If you want to handle both forms with a single view then you can use the name attribute in your submit button inside your template.
template:
<form method='POST'>
{{p_form.as_p}}
<button type="submit" name="btn1">Add Product</button>
</form>
<form method='POST'>
{{c_form.as_p}}
<button type="submit" name="btn2">Add Category</button>
</form>
Now in the views.
def addNewProduct(request):
p_form = productDetailsForm()
c_form = productCategoryForm()
if request.method=='POST' and 'btn1' in request.POST:
p_form=productDetailsForm(request.POST)
if p_form.is_valid():
p_form.save()
return redirect("/")
if request.method=='POST' and 'btn2' in request.POST:
c_form=productCategoryForm(request.POST)
if c_form.is_valid():
c_form.save()
return redirect("/")
context = {
"p_form": p_form,
"c_form": c_form
}
return render(response, 'purchases.html', context)
To avoid spaghetti code I usually separate the views:
# views.py
def purchases(response):
form = productCategoryForm()
detailsForm = productDetailsForm()
return render(response, 'purchases.html', {"form": form, "detailsForm": detailsForm})
#require_POST
def add_category(request):
form = productCategoryForm(response.POST)
if form.is_valid():
a = form.cleaned_data["productCateg"]
b = productCategory(productCat=a)
b.save()
return HttpResponseRedirect("/acctg/purchases/")
#require_POST
def add_product_details(request):
form = productDetailsForm(response.POST)
if form.is_valid():
# your logic here
b.save()
return HttpResponseRedirect("/acctg/purchases/")
# urls.py
path('purchases/', purchases, name='purchases'),
path('add_category/', add_category, name='add_category'),
path('add_product_details/', add_product_details, name='add_product_details'),
# purchases.html
<form action="{% url 'add_category' %}" method="post">
{% csrf_token %}
{{form}}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<form action="{% url 'add_product_details' %}" method="post">
{% csrf_token %}
{{detailsForm}}
<button type="submit" class="btn btn-primary">Submit</button>
</form>

File upload field doesn't display (Django)

I have a django form, but it's not showing the "upload file" field when I render it on my website. What am I doing wrong? Ideally, the form has a question ID associated with it that's submitted automatically (e.g. it doesn't have to be manually put in by the user when uploading the file)
models.py
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
response_file = models.FileField(upload_to='audio_responses')
forms.py
class PostAudio(forms.ModelForm):
class Meta:
model = Choice
fields = ('response_file',)
views.py
def QuestionRecordSubmitView(request, pk):
model = Question
if request.method == 'POST':
form = PostAudio(request.POST, request.FILES)
if form.is_valid():
form.instance.question_id = pk
form.save()
# get survey pk
question_instance = Question.objects.get(pk=pk)
survey_pk = question_instance.survey.pk
return redirect('survey-share',pk=survey_pk)
else:
form = PostAudio()
return render(request, 'survey/question_audio_submit.html')
html
{% extends "landing/base.html" %}
{% block content %}
<h2>New Submission</h2>
<form method="POST" class="post-form" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock content %}
def QuestionRecordSubmitView(request, pk):
model = Question
if request.method == 'POST':
form = PostAudio(request.POST, request.FILES)
if form.is_valid():
form.instance.question_id = pk
form.save()
# get survey pk
question_instance = Question.objects.get(pk=pk)
survey_pk = question_instance.survey.pk
return redirect('survey-share',pk=survey_pk)
else:
form = PostAudio()
return render(request, 'survey/question_audio_submit.html', {'form':form})```

Django - filter drop down choices based on user group

i am trying to filter a forms drop down list based on a users group
To find the user group i am using a custom templatetag
template tag
from django import template
register = template.Library()
#register.filter(name='in_group')
def in_group(user,group_name):
try:
group=Group.objects.get(name=group_name)
except Group.DoesNotExist:
return False
return group in user.groups.all()
task.html
{% load group_check %}
<form method="post">
{% csrf_token %}
{% if user.is authenticated %}
{% if requset.user|in_group:'DEVELOPER' %}
#...DO SOMETHING
{{ form.as_p }}
<button type="submit">add task</button>
</form>
models
GOALS_TYPE= (('DT','Daily Task'),
('WT','Weekly Task'),
('V','Verified'),
('D','Done'),
)
class GoalStatus(models.Model):
title = models.CharField(max_length=254, null=True)
task_id=models.IntegerField(default=1,null=False)
description =models.CharField(max_length=254)
verified_by=models.ForeignKey('ScrumyUser', on_delete= models.CASCADE, null=True)
status=models.CharField(choices=GOALS_TYPE, max_length=2, default='DT')
def __str__(self):
return self.title
the template for the form is based on the forms.py
forms.py
class ChangeTaskForm(forms.ModelForm):
class Meta:
model = GoalStatus
fields = ('title', 'task_id','description','status', 'verified_by')
views.py
def move_goals(request,pk):
if request.method == 'POST':
form = ChangeTaskForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/index/')
else:
form = ChangeTaskForm()
return render(request, 'oderascrumy/task.html', {'form': form})
urls.py
path('task/<pk>/', views.move_goals, name='move_goals')
so for example if the user is in group "developer", the drop down choices for status will be only verified and done
You can do like below
views.py
def move_goals(request,pk):
if request.method == 'POST':
form = ChangeTaskForm(request.POST, user=request.user)
if form.is_valid():
return HttpResponseRedirect('/index/')
else:
form = ChangeTaskForm(user=request.user)
return render(request, 'oderascrumy/task.html', {'form': form})
forms.py
class ChangeTaskForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super(ChangeTaskForm, self).__init__(*args, **kwargs)
if user.groups.filter(name='DEVELOPER').exists():
self.fields['status'].choices = (('V','Verified'), ('D','Done'),)
class Meta:
model = GoalStatus
fields = ('title', 'task_id','description','status', 'verified_by')
template.html
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">add task</button>
</form>
I think no need of template tag for this.

trouble with subscribing form (Django)

Please, help.. I do not udersatand what I should do this code to work..
home.html:
<form class="subscribe-form" action="{% url 'subscribe' %}" method="POST">{% csrf_token %}
{{ form }}
<button class="btn btn-main btn-lg" type="submit">Подписаться!</button>
</form>
{% if success %}
<div class="subscribe-result">
{{ success }}
</div>
{% endif %}
urls.py:
url(r'^$', 'interior_app.views.home', name='home'),
url(r'^subscribe/$', 'interior_app.views.subscribe', name='subscribe')
models.py:
class Subscriber(models.Model):
email = models.EmailField('', max_length=100, null=True, blank=True)
forms.py:
class SubscriberForm(forms.ModelForm):
class Meta:
model = Subscriber
fields = ['email']
admin.py:
class SubscriberAdmin(admin.ModelAdmin):
list_display = ('email',)
admin.site.register(Subscriber, SubscriberAdmin)
views.py:
def home(request):
portfolios = PortfolioObject.objects.all()
photos = []
for portfolio in portfolios:
for obj in portfolio.photo_set.all():
photos.append(obj)
form = SubscriberForm()
context = {"photos": photos[::2], "form": form}
return render(request, "home.html", context)
def subscribe(request):
print request
success = ''
if request.method == "POST":
print request.POST
form = SubscriberForm(request.POST)
print form
if form.is_valid():
form.save()
success = "Ваш Email успешно отправлен"
form = SubscriberForm()
else:
form = SubscriberForm()
context = {"photos": photos[::2], "form": form, "success": success}
return render(request, "home.html", context)
I input email in the form, push button and nothing is happening.
Any data in admin, any {{ success }}.
I need not AJAX.. I would like to do this feature only with Django

Categories