I am trying to show a list of user emails in my user profile template, which seems like a really simple thing to do, but I can't get anything to display.
I've tried using {{ email }} in my loop, but it just returns the current users email a bunch of times.
I've also used {{ emailAddress.email }} which doesn't return anything.
My user profile template:
{% extends "base.html" %}
{% block content %}
<title>{% block title %} | {{ username }}{% endblock %}</title>
<h2>{{ username }}</h2>
<p><b>Location: </b>{{ user.profile.location }}</p>
<p><b>Email: </b>{{ email }}</p>
<p>
{% for emailAddress in emailList %}
{{ user.email }}
{% endfor %}
</p>
Edit Profile
{% endblock content %}
My user profile view:
def loggedin(request):
return render_to_response('loggedin.html',
{'username':request.user.username,
'user':request.user, 'email':request.user.email,
'emailList':User.objects.values_list('email', flat=True)})
Something's wrong with your for loop.
{% for emailAddress in emailList %}
{{ user.email }}
{% endfor %}
You are looping through emailList and every element is assigned to a variable emailAddress. Your loop should look like this.
{% for emailAddress in emailList %}
{{ emailAddres }}
{% endfor %}
Note that emailList is already a list of emails so you don't need to do emailList.email.
Related
One of functionality in my training project:
subscribe to the news by check-box and e-mail.
Send newsletter daily.
The user can unsubscribe from the mailing list in his profile by unchecking the checkbox.
It so happened that first I set up a daily newsletter for users who have booleanfield = true.
For it I marked the checkboxes in the admin panel. It works.
Now it is necessary to add the checkbox and the mail field to the news page.
I'm stuck on the simplest. Tired and confused.
Please help me place a checkbox and a mail box with a send button on the news page
models.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
hr = models.BooleanField(default=False)
subscribed_for_mailings = models.BooleanField(default=False)
subscription_email = models.EmailField(default="")
def __str__(self):
return str(self.user)
Forms.py
class MailingForm(forms.ModelForm):
class Meta:
model = models.Profile
fields = ('subscription_email', 'subscribed_for_mailings', )
widgets = {
'subscription_email': forms.EmailInput(attrs={"placeholder": "Your Email..."}),
'subscribed_for_mailings': forms.CheckboxInput,
}
views.py
def all_news(request):
today = date.today()
today_news = models.TopNews.objects.filter(created__gte=today)
return render(request, "news.html",
{'today_news': today_news})
def mailing_news(request):
if request.method == 'POST':
mailing_form = forms.MailingForm(request.POST)
if mailing_form.is_valid():
mailing_form.save()
return HttpResponse('You will receive news by mail')
else:
mailing_form = forms.MailingForm()
return render(request, "news.html", {'mailing_form': mailing_form})
urls.py
...
path('news/', views.all_news, name='all_news'),
...
news.html
{% extends 'base.html' %}
{% block title %}
News
{% endblock %}
{% block body %}
<h1>Last news</h1>
{% for news in today_news%}
<h3>{{ news.title }}</h3>
Read this news
<p>
{{ news.created }}
</p>
<hr>
{% endfor %}
<h4>I want to receive news by mail</h4>
<form action="." method="post">
{{ mailing_form.as_p }}
{% csrf_token %}
<label>
<input type="submit" value="Subscribe">
</label>
</form>
{% endblock %}
The page displays a list of news and only the "send" button. There is no check-box and a field for mail
enter image description here
Finally I realized this functionality in a different way:
forms.py
class MailingForm(forms.ModelForm):
class Meta:
model = models.Profile
fields = ('subscribed_for_mailings', 'subscription_email', )
views.py
#login_required
def mailing_news(request):
if request.method == "POST":
mailing_form = forms.MailingForm(request.POST,
instance=request.user.profile,
)
if mailing_form.is_valid():
mailing_news = mailing_form.save(commit=False)
mailing_news.subscribed_for_mailings = mailing_news.subscribed_for_mailings
mailing_news.subscription_email = mailing_news.subscription_email
mailing_news.save()
return render(request, "subscribe_complete.html",
{"mailing_news": mailing_news})
else:
mailing_form = forms.MailingForm()
return render(request, 'subscribe.html', {"mailing_form": mailing_form})
news.html
{% extends 'base.html' %}
{% block title %}
News
{% endblock %}
{% block body %}
<h1>Last news</h1> {{ news.created }}
{% for news in today_news%}
<h3>{{ news.title }}</h3>
Read this news
<hr>
{% endfor %}
I want to receive news by mail
{% endblock %}
urls.py
...
path('subscribe/', views.mailing_news, name='subscribe')
...
news.html
{% extends 'base.html' %}
{% block title %}
News
{% endblock %}
{% block body %}
<h1>Last news</h1> {{ news.created }}
{% for news in today_news%}
<h3>{{ news.title }}</h3>
Read this news
<hr>
{% endfor %}
I want to receive news by mail
{% endblock %}
subscribe.html
{% extends 'base.html' %}
{% block title %}
Subscribe
{% endblock %}
{% block body %}
<form action="." method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ mailing_news.as_p }}
{% if user.profile.subscribed_for_mailings is True %}
<input type="checkbox" name="subscribed_for_mailings" id="id_subscribed_for_mailings" checked="">
If you don't want to receive emails anymore, uncheck
<br>
Subscription email: <input type="email" name="subscription_email" value={{ user.profile.subscription_email }} class="vTextField" maxlength="254" id="id_subscription_email">
{% else %}
<label>
<input type="checkbox" name="subscribed_for_mailings" id="id_subscribed_for_mailings">
I want to subscribe for mailing news
</label>
<p><label>
Send news on my email:
<input type="email" name="subscription_email" class="vTextField" maxlength="254" id="id_subscription_email">
</label></p>
{% endif %}
<p><input type="submit" value="Update"></p>
</form>
{% endblock %}
subscribe_complete.html
{% extends 'base.html' %}
{% block title %}
Subscribing complete
{% endblock %}
{% block body %}
<h3>Hi {{ user.username }}</h3>
Thanks for subscribing.
You will receive daily news by email: {{ user.profile.subscription_email }}
{% endblock %}
you need to change subscribed_for_mailings in mailing news, like this
def mailing_news(request):
if request.method == 'POST':
mailing_form = forms.MailingForm(request.POST)
if mailing_form.is_valid():
profile = mailing_form.save(commit=False) ####
profile.subscribed_for_mailings = mailing_form.cleaned_data.get('subscribed_for_mailings') ####
profile.subscription_email = mailing_form.cleaned_data.get('subscription_email') ####
profile.save() #### new_line
return HttpResponse('You will receive news by mail')
else:
mailing_form = forms.MailingForm()
return render(request, "news.html", {'mailing_form': mailing_form})
you can change in cleaned_data.get('....')
Hello I just started learning django and I am having problems rending the content the way I want to. Ideally I want to manage a system of tickets and have the ticket number, start-date, end-date and status appear on the page. Similar to the below format
{% extends 'base.html' %}
{% block content %}
<h1>test</h1>
{% for cr in crs %}
<p>{{ cr }}</p>
{% endfor %}
{% endblock content %}
From what I have in my views file the only thing being displayed when i view it in my web browser is only the ticket number.
Any help would be appreciated.
You should render the attributes .start_date and .end_date in your template as well, so:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date }} {{ cr.end_date }} {{ cr.get_status_display }}
{% endfor %}
You can specify the date format with the |date template filter [Django-doc]:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date|date:"Y-m-d" }} {{ cr.end_date|date:"Y-m-d" }} {{ cr.get_status_display }}
{% endfor %}
I have troubles with django allauth
Here's my template
password_reset_key_message.txt
{% autoescape off %}
{% load static from staticfiles %}
{% load i18n %}Hello{% if username %}, {{ username }}{% endif %}!
{% if username %}{% blocktrans %}In case you forgot, your username is {{ username }}.{% endblocktrans %}{% endif %}
You're receiving this e-mail because you or someone else has requested a password for your user account.
It can be safely ignored if you did not request a password reset. Click the link below to reset your password.
{% load static %}
<img src="{% static "avatar.jpg" %}" alt="avatar" />
{{ password_reset_url }}
{% if username %}{% blocktrans %}In case you forgot, your username is {{ username }}.{% endblocktrans %}
{% endif %}{% blocktrans with site_name=current_site.name site_domain=current_site.domain %}Thank you for using {{ site_name }}!
{{ site_domain }}{% endblocktrans %}
{% endautoescape %}
I'm trying to add image in email body (already trying with link on source and converting image to base64) but got same result
But when I click on reset_email I got this result:
I would create a condition {% if ... %} that allows me to compare the {{ looged_user }} with the user's response {{ reply.user }}
The looged_user is the person connect
Reply.user is the response of a person
I would show only person logged answers
So I have try:
{% if '{{ logged_user }}' == '{{ reply.user }}' %}
or
{% if {{ logged_user }} == {{ reply.user }} %}
or
{% if logged_user == reply.user %}
But nothing works!
Nevertheless the logged user is equivalent to reply.user ...
My template :
{% for question in questions %}
{% for reply in question.reply_set.all %}
<hr>
{{ logged_user }}<br>{{ reply.user }} --> {{ question }} : {{ reply.answer }}
{% endfor %}
{% endfor %}
you can see that they are well equal ..
The result of the screenshot:
when I try to add the condition this shows me nothing.
I have made it possible for users on my website to upload a post, and to see all the other posts from other users as well. The below code attaches the user, who wrote the post, userpicture.
I wanted it to be a link to that user. My problem is that the below code links to the current user, and not the user which created the post.
Anyone who has some ideas of how to fix this? Thank you!
{% if item.sender.userpicture_set.all %}
{% for item in item.sender.userpicture_set.all %}
{% if item.active %}
{% if forloop.first %}
{% if forloop.last %}
<a href='/members/{{ user.username }}'><img src='{{ MEDIA_URL }}{{ item.image }}' class='img-responsive' id='post-userpicture'/></a>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<small><a href='/members/{{ user.username }}'>{{ item.sender }}</a><span style='color: grey;'> {{ item.sent }}</span></small>
{% endif %}
In your item model, add a field username, which stores the username of the user who made this post.
Here, it creates a link to the current user, because "user" doesn't have any information about the post. It has information about the current user.
After adding the 'username' field,
{% if item.sender.userpicture_set.all %}
{% for item in item.sender.userpicture_set.all %}
{% if item.active %}
{% if forloop.first %}
{% if forloop.last %}
<a href='/members/{{ item.sender.username }}'><img src='{{ MEDIA_URL }}{{ item.image }}' class='img-responsive' id='post-userpicture'/></a>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
<small><a href='/members/{{ item.sender.username }}'>{{ item.sender }}</a><span style='color: grey;'> {{ item.sent }}</span></small>
{% endif %}
UPDATE:
I have change to item.sender.username. This should work.
The problem is that you are referencing the user object in your link as <a href='/members/{{ user.username }}'>, the correct url should be something like <a href='/members/{{ item.sender.username }}'> but it requires you to have a ForeignKey reference to the user in the item model.