I want to allow only manager to create view a content on my website, So I added an entry to my model Profile
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
manager = models.BooleanField(default=False)
By default is false
If false the user cannot see the content.
So I tried :
{% if profile == manager %}
SHOW THE CONTENT
{% else %}
Does not show the content
{% endif %}
But nothing change.
What did I do wrong ?
You need to do it like that :
{% if user.profile.manager %}
SHOW THE CONTENT
{%else%}
Does not show the content
{% endif %}
You forgot to add user since it is connected
You added a property on your model.
Your Profile object won't == manager. You need to access the property on your Profile object.
Try
{% if profile.manager %}
This will check is that profile object's manager property is True
If you have not overridden your view context you need to access your fields with object.
{% if object.manager %}
SHOW THE CONTENT
{%else%}
Does not show the content
{% endif %}
Related
verified = models.BooleanField(default=False)
I want to show only that objects in frontend whose verified field is true in Django models.
There are many ways
you can handle this on your views
in views.py
modelList = modelname.objects.filter(verified=True)
also you can handle it on HTML
in views.py
modelList = modelname.objects.all()
in html
{% for models in modelList %}
{% if models.verified == True %}
# Your Code
{% endif %}
{% endfor %}
You filter the items with:
MyModel.objects.filter(verified=True)
with MyModel the model that contains the verified field.
you have to ways to achive that that either it with your views or html
first views
you can filter your model to return only object which is verfied like this
name = modelname.objects.filter(verified=True)
second way
or you can pass in html while you are requesting all object of that field in views
in views
name = modelname.objects.all()
then in html while fetching data
{% for name in models %}
{% if name.verified == True %}
then pass the object which are verified
{% else %}
pass another data
{% endif %}
{% endfor %}
i hope now you got my point tell me if you got any error while implementing any of these code
I have built a model which have a OneToOne relation with the User object in Django like this :
class Student(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
But in the HTML file, the filter {% if user.student.is_authenticated %} does not work but the filter {% if user.is_authenticated %} works. I thought that the Student class inherits the attributes from the User class.
Is there an other possibility to create custom users from the User class with the possibility to use {% if user.student.is_authenticated %} ? I want also to have the possibility to use for example {% if user.teacher.is_authenticated %}.
I thought that the Student class inherits the attributes from the User class.
No, it does not inherit, these are just two models (tables) where one of the tables refers to the others by specifying the primary key.
You thus check this with:
{% if user.is_authenticated %}
…
{% endif %}
or if you want to know whether the user of a student is authenticated, you can work with:
{% if mystudent.user.is_authenticated %}
…
{% endif %}
I'm trying to make individual pages for each author showing their name and posts. I can't seem to get the username displayed.
views.py
class UserProfileView(generic.ListView):
template_name = 'howl/user-profile.html'
context_object_name = 'user_howls'
def get_queryset(self):
author = self.request.user
u = User.objects.get(username=author)
return Howl.objects.filter(author=u)
models.py
class Howl(models.Model):
author = models.ForeignKey(User, null=True)
content = models.CharField(max_length=150)
Here is where I'm stuck.
user-profile.html
{% extends 'howl/base.html' %}
{% block content %}
<h1>User: {{user_howl.author}}</h1>
{% for user_howl in user_howls %}
<ul>
<li>{{user_howl.content}}</li>
</ul>
{% endfor %}
{% endblock %}
The content is displayed just fine, but the heading just says "User: ", how do I give it a context without using a for loop?
I've tried:
{% for author in user_howls.author %}
<h1>User: {{author}}</h1>
{% endfor %}
and
{% if user_howls.author %}
<h1>User: {{user_howl.author}}</h1>
{% endif %}
Still the same outcome, displaying "User: "
user_howls is a queryset so it won't have an author attribute, you need to get the author of the iterated object
{% for howl in user_howls %}
<h1>User: {{ howl.author}}</h1>
{% endfor %}
More to the point though, it doesn't make sense to start from a Howl list, when you are just returning the results for the user_profile, nor does it make sense to use a ListView. so instead, start from the user and then look up its howls
user_obj.howl_set.all()
Since your queryset is based on the posts belonging to the current user, you can shortcut all of this and just show the user directly:
User: {{ user }}
I've got a Django model that looks like this
from django.contrib.auth.models import User
class Cohort(models.Model) :
cohort_name = models.CharField(max_length=64, primary_key=True)
cohort_description = models.TextField(null=False)
creation_date = models.DateTimeField(default=now)
class CohortMembers(models.Model) :
cohort = models.ForeignKey(Cohort)
member = models.ForeignKey(User)
creation_date = models.DateTimeField(default=now)
So you can see there is a many-to-many relationship between Cohorts and Users.
In a template, I am listing Cohorts as such (simplified to give you the idea):
{% for cohort in object_list %}
{{ cohort.cohort_name }}
<!-- list all members of the cohort -->
{% for cohortmember in cohort.cohortmembers_set.all %}
{% if request.user.username == cohortmember.member.username %}
<!-- the user is a member of the cohort, provide a delete button -->
<button>Leave cohort</button>
{% else %}
Some other user called {{ cohortmember.member.username }} is a member.
{% endif %}
{% endfor %}
{% endfor %}
What I need to do, is provide a button for the user to join the cohort if they are not already a member. In the most primitive of python, you might do this;
# clearly some better python would be to use a django model query, rather
# than iterate over the members, but this encapsulates the basic logic
is_member = False
for cohortmember in cohort.cohortmembers_set.all:
if request.user == cohortmember.member:
is_member = True
if is_member :
# a leave button
else:
# a join button
Can I do any of that in the Django template language or do I have to resort to putting code like that into the View class?
The only real way I have found to do this is a combination of manipulating the CohortView and a simple bit of template programming.
I turned the question around: not to ask "is this user a member of that cohort?" but made it into "is this cohort in the list of the user's memberships?" in this simple way.
In the CohortListView's get_context_data as follows:
def get_context_data(self, **kwargs) :
context = super(CohortListView, self).get_context_data(**kwargs)
memberships = CohortMembers.objects.filter(member=self.request.user)
cohorts = []
for membership in memberships:
cohorts.append(membership.cohort)
context['memberships'] = cohorts
return context
Note I'm abandoning the many to many object and just storing a list of Cohorts the user is a member of directly in the context.
Then, in the template:
{% for cohort in object_list %}
{% if cohort in memberships %}
<!-- leave button -->
{% else %}
<!-- join button -->
{% endif %}
{% endfor %}
I am writing a django template Configuration_Detail.html and it renders correctly on the relevant urls. It does not, however, take any variables whatsoever from the view class. I had a very similar template Configuration_List.html which worked fine, though that was a ListView not a DetailView.
Configuration_Detail.html:
{% extends "base.html" %}
{% load i18n %}
{% block title %}{% trans 'MySite Database' %}{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'MySite Database: Current Instrumentation Configuration' %}</h1>
{% endblock %}
{% block content %}
Here is some text {{name}} with a variable in the middle.
{% endblock %}
The page renders the title bar fine, but the content block becomes "Here is some text with a variable in the middle."
I believe it should be taking the variable {{ name }} from here.
views.py:
class ConfigurationDetail(DetailView):
model = Configuration
def getname(self):
name = 'debug'
return name
But it does not...
Any suggestions on how to fix this would be greatly appreciated.
Edited to add:
Models.py - Configuration:
class Configuration(models.Model):
title = models.CharField(max_length=100,unique=True,blank=False)
author = models.ForeignKey(User)
created = models.DateField("date created",auto_now_add=True)
modified = models.DateField("date modified",auto_now=True)
description = models.CharField(max_length=512)
drawing = models.ForeignKey(Drawing,blank=True,null=True)
instruments = models.ManyToManyField(Instrument)
def __unicode__(self):
return self.title
The get_context_data() method is using ctx['author'] = Configuration.author
For DetailView, an object variable is added in the context which points to the database object for which the view is being rendered. So in your template you can do:
{% block content %}
Here is some text {{ object.author.get_full_name }}
with a variable in the middle.
{% endblock %}
The get_full_name method is from the User object.
if i understood correctly you need to access a model property from within a template, but for that is sufficient to do {{ configuration.author }} without modifying the context data at all!
the DetailView puts in the context the selected model, accessible with the dot notation.