Using Check boxes in Taggit - python

I am trying to use Taggit for users to be able to tag their posts while they are submitting a form. But I can successfully let them type manually(their tags), I am trying to change to check boxes. Any ideas?
forms.py
class TalesForm(ModelForm):
class Meta:
model = Tale
fields = ('title', 'body', 'tags')
m_tags = TagField()
models.py
class Tale(models.Model):
title = models.CharField(max_length = 50)
body = models.TextField(max_length = 10000)
pub_date = models.DateTimeField(default = datetime.now)
poster = models.CharField(max_length = 30)
tags = TaggableManager()
def __unicode__(self):
return self.title
views.py
def add(request):
if request.user.is_authenticated():
if request.method=='POST':
form=TalesForm(request.POST)
if form.is_valid():
m_tags = form.cleaned_data['tags']
newTale=Tale(title=form.cleaned_data['title'], body=form.cleaned_data['body'], poster = request.user)
newTale.save()
for m_tag in m_tags:
newTale.tags.add(m_tag)
#form.save_m2m()
return HttpResponseRedirect('/home/%s'%newTale.id)
else:
return render_to_response('story/add.html', {'form':form}, context_instance=RequestContext(request))
else:
form=TalesForm()
args = {}
args.update(csrf(request))
args['form'] = form
context= {'form': form}
return render_to_response('story/add.html',context, context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/home')
html
<form method="post" action="/home/add/">
{% csrf_token %}
<div class="register_div">
{% if form.title.errors %}<p class="error" >{{ form.title.errors }}</p>{% endif %}
<p><label for="title"{% if form.title.errors %} class="error"{% endif %}>Title:</label></p>
<p>{{ form.title}}</p>
</div>
<div class="register_div">
{% if form.body.errors %}<p class="error" >{{ form.body.errors }}</p>{% endif %}
<p><label for="body"{% if form.body.errors %} class="error"{% endif %}>Body:</label></p>
<p>{{ form.body }}</p>
</div>
<div class="register_div">
<p><label for="tag">Tags:</label></p>
<p>{{ form.tags }}</p>
</div>
<input type="submit" value="Add Story" name="submit" />
</form>

Define your form this way:
from taggit.models import Tag
class TalesForm(ModelForm):
tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
widget=forms.CheckboxSelectMultiple())
class Meta:
model = Tale
fields = ('title', 'body', 'tags')

Related

Django search / filter across multiple models

In my Django website, I'm trying to create a search bar on my homepage that returns results that are stored in two different models(Articles, Jobs)
Currently, I get an empty result set when I search using this code:
In models.py,
class Articles(models.Model):
objects = None
news_title = models.CharField(db_column="title", max_length=400)
news_url = models.URLField(db_column="link", max_length=400)
image_link = models.CharField(db_column="image", max_length=400)
published_date = models.CharField(db_column="published_date", max_length=400)
class Meta:
managed = False
db_table = "articles"
def __str__(self):
return self.news_title
class Jobs(models.Model):
objects = None
company = models.CharField(db_column='company', max_length=100)
job_title = models.CharField(db_column='job_title', max_length=300)
experience = models.CharField(db_column='experience', max_length=300)
edu_level = models.CharField(db_column='edu_level', max_length=50)
class Meta:
managed = False
db_table = "job_list"
def __str__(self):
return self.job_title
In views.py,
class SearchView(ListView):
template_name = 'blog/search_results.html'
def get_queryset(self):
request = self.request
query = request.GET.get('q', '')
articles_results = Articles.objects.filter(Q(news_title__icontains=query))
jobs_results = Jobs.objects.filter(Q(job_title__icontains=query))
context={
'articles':articles_results,
'jobs':jobs_results,
}
return render(request, 'blog/search_results.html', context)
In main_view.html, I'm using this code for creating a search bar:
<form action="{%url 'search_results' %}" method="get" values="{{request.GET.q}}" class="search-jobs-form">
<div class="row mb-5">
<input name="q" type="text" values="{{request.GET.q}}" placeholder="search">
</div>
<button type="submit">Search</button>
</form>
And in search_results.html,
{% block content %}
{% for job in jobs %}
<h5>{{job.job_title}}</h5>
<p>{{job.company}}</p>
{% endfor %}
{% for article in articles %}
<h5>{{article.news_title}}</h5>
<p>{{article.published_date}}</p>
{% endfor %}
{% endblock %}
I ended up not using class SearchView(ListView) in views.py and made it work
Here's the code that I used
#index.html Search Bar
<form method="get" class="search-jobs-form" action="{% url 'main_search'%}">
{% csrf_token %}
<input type="text" placeholder="Job title, Company..." name="search_query">
<button type="submit" class="btn btn-info btn-lg btn-block text-white btn-search">Search Job</button>
</form>
In views,
#views.py
def main_search(request):
q = request.GET['search_query']
articles = Articles.objects.filter(news_title__icontains=q)
jobs = Jobs.objects.filter(Q(job_title__icontains=q) | Q(company__icontains=q))
return render(request, "blog/main_search.html", {'articles':articles, 'jobs':jobs, 'page_name':'Search Results', 'q':q})

how to display specific results from the dropdown in models.py in django

I want to display only employees which emp_type is 'Doctor'?
** Here is Models.py **
class Employee(models.Model):
name = models.CharField(max_length=50)
emp_type_choices = [
('Nurse', 'Nurse'),
('Doctor', 'Doctor'),
('Other', 'Other'),
]
emp_type = models.CharField(
max_length=6, choices=emp_type_choices, default='Nurse')
def __str__(self):
return self.name
class Ticket(models.Model):
patient = models.CharField(max_length=50)
doctor = models.ForeignKey(Employee, on_delete=models.CASCADE)
def __str__(self):
return self.patient.name
This is my Forms.py
class TicketModelForm(forms.ModelForm):
class Meta:
model = Ticket
fields = ['doctor', 'status']
widgets = {
'doctor': forms.Select(attrs={'class': 'form-control','placeholder': 'Doctor Name'}),
}
This is my Views.py
#login_required
def TicketToGenerateView(request, pk):
ticket = get_object_or_404(Patient, pk=pk)
form = TicketModelForm(request.POST or None)
if form.is_valid():
obj.save()
return redirect('/dashboard/ticket')
context = {
'form': form,
'ticket': ticket,
}
return render(request, 'dashboard/ticket.html', context)
This is my Template
<form action="." method="POST">
{% csrf_token %}.
{% for field in form %}
<div class="form-group">
{{ field }}
{% if field.errors %}
{% for error in field.errors %}
<p class="text-danger">{{ error|escape }}</p>
{% endfor %}
{% endif %}
</div>
{% endfor %}
<div class="form-group float-right">
<button type="submit" class="btn btn-success btn-sm" value=" {{ valueBtn }} "> <span
class="glyphicon glyphicon-plus"></span> </button>
</div>
</form>
In the template, I'm displaying all registered employees as a dropdown list, but I would like to display only employees which their emp_type is 'Doctor'.
Also the Admin site I would like to see the only emp_type which are 'Doctor'.
Thanks
# You have to use filter for your query
emp_doctor = Employee.objects.filter(emp_type='Doctor')
print(emp_doctor)

django modelform how to know input type is checkbook?

My modelform is a dynamically generated modelform,I want to know the type of is_true in the modelForm. The type of the input tag is the checkbook type.
If I know the type=‘checkbox’ of the is_true field, add a class attr to him separately.
The default type='checkbox’ interface is too ugly
models
class Employee(AbstractBaseUser):
"""
用户表
"""
username = models.CharField(max_length=30, verbose_name='姓名')
email = models.EmailField(verbose_name='邮箱', unique=True)
is_true = models.BooleanField(default=False, verbose_name='是否超级用户')
views
class ModelFormDemo(ModelForm):
class Meta:
model = self.model
if self.list_editable:
fields = self.list_editable
else:
fields = '__all__'
excluded = self.excluded
def __init__(self, *args, **kwargs):
super(ModelFormDemo, self).__init__(*args, **kwargs)
def add_view(self, request):
form = ModelFormDemo()
if request.method == "POST":
res_dict = {'status': 1, 'msg': 'success'}
form = ModelFormDemo(request.POST)
if form.is_valid():
obj = form.save()
else:
res_dict['msg'] = form.errors
res_dict['status'] = 2
return JsonResponse(res_dict)
return render(request, "xadmin/add_view.html", locals())
html
<form class="layui-form" method="post">
{% csrf_token %}
{% for field in form %}
{% if field.name == 'employee' %}
<input type="hidden" name="employee" value="{{ user.id }}">
{% else %}
<div class="layui-form-item">
<label class="layui-form-label">{{ field.label }}</label>
<div class="layui-input-inline">
{{ field }}
</div>
</div>
{% endif %}
{% endfor %}
<div class="layui-form-item">
<div class="layui-input-block">
<input type="button" class="layui-btn" lay-filter="add" lay-submit="" value="add">
</input>
<button type="reset" class="layui-btn layui-btn-primary">reset</button>
</div>
</div>
</form>
You can use the Widget.attrs arg in your form __init__ method.
https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs

django two ModelForms with same field name on one template

I have two models and both have field 'status' which has different meaning for them.
class Order(models.Model):
...
status = models.PositiveIntegerField(default=0, choices=ORDER_STATUSES)
...
class ACS(models.Model):
status = models.IntegerField(default=-1, choices=STATUSES)
order = models.ForeignKey(Order, blank=True, null=True)
...
Their forms looks like:
class ACSForm(forms.ModelForm):
status = forms.ChoiceField(
choices=STATUSES,
widget=forms.Select(attrs={'class': 'form-control'})
)
...
class Meta:
model = ACS
fields = ('status',)
class OrderACSEditForm(forms.ModelForm):
status = forms.ChoiceField(
choices=ORDER_STATUSES,
widget=forms.Select(attrs={'class': 'form-control'})
)
class Meta:
model = Order
fields = ('status',)
I want to edit both this fields on the same page. My view.py looks like
def edit(request, item_id=""):
data = ACS.objects.get(pk=item_id)
form = ACSForm(instance=data)
order = Order.objects.get(id=data.order.id)
form_edit = OrderACSEditForm(instance=order)
if request.POST:
form = ACSForm(request.POST, instance=data)
form_edit = OrderACSEditForm(request.POST)
if form.is_valid() and form_edit.is_valid():
form_edit.save()
obj = form.save()
messages.add_message(request, messages.SUCCESS, 'Your data successfully saved.')
if request.POST['action'] == "save_stay":
return redirect("/panel/packages/acs/edit/" + str(obj.id))
else:
return redirect("/panel/packages/acs/")
return render(request, 'ui/packages/acs/edit.html', dict(data=data, form=form, form_edit=form_edit, item_id=item_id))
And template:
<div class="form-group {% if form.status.errors %}has-error{% endif %}">
<label>{% trans "Status" %}</label>
{% if form.status.errors %}
{% for error in form.status.errors %}
<label class="control-label">{{ error }}</label>
{% endfor %}
{% endif %}
{{ form.status }}
</div>
<div class="form-group {% if form_edit.status.errors %}has-error{% endif %}">
<label>{% trans "Order status" %}</label>
{% if form_edit.status.errors %}
{% for error in form_edit.status.errors %}
<label class="control-label">{{ error }}</label>
{% endfor %}
{% endif %}
{{ form_edit.status }}
</div>
But in result form.status gets values from form_edit.status which is not correct. I need to solve this problem without changing names of model fields but don't know how.
Use the prefix argument for your forms, to namespace the field names.
form = ACSForm(prefix='acs', instance=data)
form_edit = OrderACSEditForm(prefix='edit', instance=order)
Remember to use the same prefix when you instantiate the form with POST data as well.

Django Class Based View Form Won't Save Data To Database

I can not get my form data to commit to my sqlite3 database. I don't see any errors. I can commit data through admin, but not through my own controller using form. I've tried many diff. combos and still no success. I would like to use class based view, please. Everything works, the form just won't save the data to database. There are no errors.
url: url(r'^create/$', CreateRequest.as_view())
forms.py:
class CreateForm(ModelForm):
date_due = forms.DateTimeField(widget=widgets.AdminSplitDateTime)
class Meta:
model = Request
fields = ['region', 'user_assigned', 'user_requester', 'description']
views.py:
class CreateRequest(LoginRequiredMixin, CreateView):
model = Request
fields = ['region', 'user_assigned', 'user_requester', 'date_due', 'description']
template_name = "requests_app/createRequest.html"
form_class = CreateForm
success_url = '/'
def form_valid(self, form):
objects = form.save()
return super(CreateRequest, self).form_valid(form)
models.py:
class Request(models.Model):
region = models.ForeignKey(Region)
completed = models.BooleanField(default=False)
user_assigned = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='user_assigned')
user_requester = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='user_requester')
date_due = models.DateTimeField()
date_completed = models.DateTimeField(null=True, blank=True)
description = models.CharField(max_length=500)
objects = models.Manager()
open_requests = OpenRequests()
completed_requests = CompletedRequests()
def mark_completed(self):
if not self.completed:
self.completed = True
self.date_completed = datetime.datetime.now()
index.html:
<h1>hi</h1>
<form action="/create/" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.region.errors }}
<label for="id_region">Region</label>
{{ form.region }}
</div>
<div class="fieldWrapper">
{{ form.user_assigned.errors }}
<label for="id_user_assigned">User Assigned</label>
{{ form.user_assigned }}
</div>
<div class="fieldWrapper">
{{ form.user_requester.errors }}
<label for="id_user_requester">user_requester: </label>
{{ form.user_requester }}
</div>
<div class="fieldWrapper">
<p> {{ form.date_due.errors.as_text }} </p>
<label for="id_date_due">Due Date</label>
{{ form.date_due }}
</div>
<div class="fieldWrapper">
{{ form.description.errors }}
<label for="id_description">Descr.</label>
{{ form.description }}
</div>
<p><input type="submit" value="Submit Request" /></p>
{% if form.non_field_errors %}
{% for err in form%}
<div class="fieldWrapper">
<p class="form-error">{{ err }}</p>
<p class="form-error">{{ err.label_tag }} {{ field }}</p>
</div>
{% endfor %}
{% endif %}
</form>
{% endblock %}
in views.py you don't need this line: objects = form.save()
It can be
class ContaktCreateView(CreateView):
model = Contakt
form_class = ContaktForm
template_name = "www/www_contakt.html"
success_url = '/thanks/'
def form_valid(self, form):
return super(ContaktCreateView, self).form_valid(form)
Also I'm not using action in form action="/create/" method="post"
You are calling this html form via your line in urls.py:
url(r'^create/$', CreateRequest.as_view())
which is using your CreateRequest view which is using your index.html form file.

Categories