I have a model that is defined like so...
class Message(models.Model):
email_subject = models.CharField(max_length=100, null=False, blank=False)
email = models.TextField(null=False, blank=False)
In want to be able to assign each of those fields two separate verbose_names(one in English, and one in Arabic). So that when I make a ModelForm, and render it to my template... I can do something like this...
{% for field in form %}
<p>
{% if field.field.required %}
<label for="{{ field.arabic_verbose_name}}" class='RequiredLabel'>{{ field.name }}</label>
{% else %}
<label for="{{ field.auto_id }}">{{ field.verbose_name}}</label>
{% endif %}
{% if field.errors %}
<div class='FieldErrorBox'>{{ field }}</div>
{% else %}
{{ field }}
{% endif %}
</p>
{% endfor %}
Is this possible?
from django.utils.translation import ugettext_lazy
class Message(models.Model):
email_subject = models.CharField(ugettext_lazy('Message field', 'email subject'), max_length=100, null=False, blank=False)
email = models.TextField(ugettext_lazy('Message field', 'email address'), null=False, blank=False)
Django internationalization can take care of this task. Coupled with a package like django-modeltranslation can often simplify the task. It's worth doing some tutorials to connect the dots. When using translatable strings in templates, there's tags (generally {% trans %}) to be used so that the correct lookups are performed.
Related
I'm a Django beginner, how can I get profile_pic from Profile Model connecting it to 'to_user' field in FriendRequest Model.
I was able to get the names of users in 'to_user' field by this:
{% for data in sent_friend_request %}
{{ data.to_user.username }}
{% endfor %}
How do I get the profile_pic for each users?
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
profile_pic = models.ImageField()
class FriendRequest(models.Model):
to_user = models.ForeignKey(settings.AUTH_USER_MODEL)
from_user = models.ForeignKey(settings.AUTH_USER_MODEL)
def following_view(request, username):
sent_friend_request = FriendRequest.objects.filter(from_user__username=username)
context = {'sent_friend_request':sent_friend_request}
{% if data.to_user.profile_pic %}
<img src="{{ data.to_user.profile_pic }}">
{% endif %}
You can do like this:
{% if data.to_user.profile.profile_pic %}
<img src="{{ data.to_user.profile.profile_pic.url }}">
{% endif %}
Hello i have 2 classes in my model:
class MO (models.Model):
many variables
class PhotoMO (models.Model):
mo = models.ForeignKey(MO, on_delete=models.CASCADE)
photo = models.ImageField(
upload_to='img/photo/%Y/%m/'
)
my views for template
class MOListView(DetailView):
template_name = 'main_site/mo_view.html'
model = models.MO
with template :
{% block content %}
<h1>{{ mo.name }}</h1>
<h3>{{ mo.phone_number }}</h3>
<h3>{{ mo.email_mo }}</h3>
<h3>{{ mo.adress_mo }}</h3>
{% for photos in mo.photomo.set.all %}
<img src="{{ photos.photo.url }}" alt="no img">
{% endfor %}
{% endblock %}
How can I display all the pictures from PhotoMO that refer to a specific entry in MO. With my "for" it dosen't work
You need to set the back relation:
class PhotoMO (models.Model):
mo = models.ForeignKey(MO, on_delete=models.CASCADE, related_name='photomo')
Then migrate the database to be able to access photomo.
Kind regards.
{% for photo in mo.photomo_set.all %}
<img src="{{ photo.photo.url }}" alt="none">
{% endfor %}
need to use mo.photomo_set.all
The issue is that Django modelformset_factory does not save to database when inputs are inserted into the forms. It appears that only else statement is reached after forms are submitted for processing. Clearly the issue is with part in views.py where if 'name' in request.POST: then do sth. Advise how to solve it would be highly appreciated. Thank you.
views.py
from django.shortcuts import render
from .forms import modelformset_factory, AssumptionsForm
from .models import Assumptions
model_names = ['Form1', 'Form2']
def get_assumptions(request):
if request.method == 'POST' and 'name' in request.POST:
formset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
if formset.is_valid():
print('valid form')
for form in formset:
if form.is_valid():
print('in for loop after valid form1')
assumptions = form.save(commit='False')
assumptions.Name = 'name'
assumptions.save()
else:
formset = modelformset_factory(
Assumptions, form=AssumptionsForm, extra=5)
print('reached else')
return render(request, 'assumptions.html', {'formset': formset, 'model_names': model_names})
models.py
from django.db import models
from django.forms import ModelForm
class Assumptions(models.Model):
Worst_Case = models.FloatField(null=True, blank=True, default=None)
Grey_Case = models.FloatField(null=True, blank=True, default=None)
Red_Case = models.FloatField(null=True, blank=True, default=None)
Blue_Case = models.FloatField(null=True, blank=True, default=None)
Green_Case = models.FloatField(null=True, blank=True, default=None)
Best_Case = models.FloatField(null=True, blank=True, default=None)
Name = models.TextField(null=True, blank=True, default=None)
forms.py
from django import forms
from django.forms import modelformset_factory, ModelForm
from .models import Assumptions
class AssumptionsForm(ModelForm):
class Meta:
model = Assumptions
fields = ['Worst_Case', 'Grey_Case', 'Red_Case', 'Blue_Case', 'Green_Case', 'Best_Case']
exclude = ['Name']
assumptions.html
<div class="form">
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
{% for name in model_names %}
<h1>{{name}}</h1>
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" name="{{name}}" value="save" />
{% endfor %}
</form>
</div>
name needs to be in your request.POST,
doublecheck your template and add an input tag with a name='name'
<input name='name' />
I have 3 connected with each other models. GroupRequirementType --> GroupRequirement --> Requirement. I am tring to show all requirements in template. Next code show me only GroupRequirementType objects. It seems like I have problems with _set.all. Whats wrong?
models.py:
class GroupRequirementType(models.Model):
name = models.CharField(_('Name'), max_length=250)
class GroupRequirement(models.Model):
group_requirement_type = models.ForeignKey(GroupRequirementType, on_delete=models.CASCADE)
name = models.CharField(_('Name'), max_length=250)
class Requirement(models.Model):
group_requirement = models.ForeignKey(GroupRequirement, on_delete=models.CASCADE)
name = models.CharField(_('Name'), max_length=250)
template:
{% for group_requirement_type in group_requirement_types %}
{{ group_requirement_type }}
{% for group_requirement in group_requirement_type.group_requirement_set.all %} <!--DONT WORK. WHY?-->
{{ group_requirement }}
{% for requirement in group_requirement.requirement_set.all %}
{{ requirement }}
{% endfor %}
{% endfor %}
{% endfor %}
It looks like Django doesn't automatically add an underscore when converting the CamelCase model name to lower case. grouprequirement_set.all works.
I'd highly recommend using Django ForeignKey related_name property in your models declaration, this makes backwards relations operations much easier: https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.related_name
In your case it will turn into something like
models.py:
class GroupRequirementType(models.Model):
name = models.CharField(_('Name'), max_length=250)
class GroupRequirement(models.Model):
group_requirement_type = models.ForeignKey(GroupRequirementType, related_name='group_requirements', on_delete=models.CASCADE)
name = models.CharField(_('Name'), max_length=250)
class Requirement(models.Model):
group_requirement = models.ForeignKey(GroupRequirement, related_name='requirements', on_delete=models.CASCADE)
name = models.CharField(_('Name'), max_length=250)
template:
{% for group_requirement_type in group_requirement_types %}
{{ group_requirement_type }}
{% for group_requirement in group_requirement_type.group_requirements.all %} <!--DONT WORK. WHY?-->
{{ group_requirement }}
{% for requirement in group_requirement.requirements.all %}
{{ requirement }}
{% endfor %}
{% endfor %}
{% endfor %}
My question is two phased but it's from the same Django Model. I am trying to get my Category model to work properly with my Petition model. Currently,
I get this 'FieldError' error when I define a queryset for my 'CategoryView' class:
Cannot resolve keyword 'created_on' into field. Choices are: description, id, petition, slug, title
Here is the code for the 'CategoryView' class:
class CategoryView(generic.ListView):
model = Category
template_name = 'petition/category.html'
context_object_name = 'category_list'
def get_queryset(self):
return Category.objects.order_by('-created_on')[:10]
Here is the code in my models.py:
class Category(models.Model):
title = models.CharField(max_length=90, default="Select an appropriate category")
slug = models.SlugField(max_length=200, unique=True)
description = models.TextField(null=False, blank=False)
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.title
def get_absolute_url(self):
return "/categories/%s/"%self.slug
class Petition(models.Model):
title = models.CharField(max_length= 90, default="Enter petition title here")
created_on = models.DateTimeField(auto_now_add=True)
image = models.ImageField(null=False, upload_to=imageupload)
video = models.CharField(max_length=600, default="Enter an external video link")
petition = models.TextField(null=False, default="Type your petition here")
created_by = models.ForeignKey(User)
category = models.ManyToManyField(Category)
def total_likes(self):
return self.like_set.count()
def __str__(self):
return self.title[:50]
def get_signatures(self):
return self.signature_set.all()
I get the 'FieldError' on my category view template (category.html) when the 'get_queryset()' has been defined.
When I comment it out, the page displays but posts are not retrieved; I get a list of the categories instead. Here is my category view template(category.html):
{% include 'layout/header.html' %}
{% load humanize %}
<div class="container content">
<div class="row">
<div class="col-md-8 post-area">
{% if category_list %}
{% for petition in category_list %}
<div class="petition-block">
<h2 class="home-title">{{petition.title}}</h2>
<span class="petition-meta">
Created {{petition.created_on|naturaltime}} by
{% if petition.created_by == user %}
You
{% else %}
#{{ petition.created_by }}
{% endif %}
{% if petition.created_by == user %}
Edit
{% endif %}
{% if petition.created_by == user %}
Delete
{% endif %}
</span>
{% if petition.image %}
<img src="{{ petition.image.url }}" alt="petition image" class="img-responsive" />
{% endif %}
</div><!--//petition-block-->
{% endfor %}
{% else %}
<p>Sorry, there are no posts in the database</p>
{% endif %}
</div>
<div class="col-md-4">
<h3>Topics</h3>
<ul>
{% for petition in category_list %}
<li>{{petition.title}}</li>
{% endfor %}
</ul>
</div>
</body>
</html>
What am I doing wrong? Please help.
That's because you are trying to order the Category's queryset by created_on and your Category model doesn't have any field called created_on. That field is within Petition model. If you want to order the categories by petition's created_on you should do a "join" between tables, but with Django's ORM is easy.
For simplicity on your code is better to name the relation
class Petition(models.Model):
...
categories = models.ManyToManyField(Category, related_name='petitions')
...
Now you can refer to the category's petitions as 'petitions'
Category.objects.order_by('-petitions__created_on')
For use the petitions within the template the best way is prefetch the petitions on the queryset to avoid hitting the database every time in the for loop.
Category.objects.prefetch_related('petitions').order_by('-petitions__created_on')
That would bring every petition for each category. So in your template you can use:
{% for category in category_list %}
{% for petition in category.petitions.all %}
...
#{{ petition.created_by }}
...
{% end for %}
{% end for %}