I want display comments from loggedin user, I tried this:
{% for c in request.user.game.comment_set.all %}
....
{% endfor %}
but doesn't work.
#edit.
Example:
User1 have comment1, comment2, User2 have comment3.
so if I log in to User1 account, I want see only comment1, comment2
The easiest way is to find user's comments in view and then pass them into your template.
views.py
user_comments = Comment.objects.filter(user=request.user)
return render(request, 'your_template.html', {'comments': user_comments)
your_template.html
{% for comment in comments %}
{{ comment.message }}
{% endfor %}
Related
I'm working with flask and have a html page that contains of user name(user.html) which took from table, Now How can I see more detail by clicking on each of users(which route to profile)?
I don't use login for app So I don't want to use g
app.py
# am I doing it right?
#app.route('/profile/<int:id>')
def profile(id=None):
detail = Contacts.query.get(id)
return render_template('profile.html', detail= detail , id=id)
user.html
{% extends "layout.html" %}
{% block content %}
<h2>show user</h2>
{% for contact in contact %}
# I got error when I click on each user name to see their 'profile'
#I guess because of id How can Solve it?
#error BuildError: ('profile', {}, None)
<strong>name:</strong><a href={{url_for('profile')}}>
{{ contact.name}}</a><br>
{% endfor %}
{% endblock %}
profile.html
{% extends "layout.html" %}
{% block content %}
<h2>show user profile</h2>
# how can I make it specific for each row of table(each user)?
{% for detail in Contacts %}
<strong>name:</strong> {{ detail.name}} <br>
<strong>email:</strong> {{ detail.email }} <br>
<strong>age:</strong> {{ detail.age}} <br>
<br>
{% endfor %}
{% endblock %}
model.py
class Contacts(db.Model):
__tablename__ = "Contacts"
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
age = db.Column(db.Integer)
submit = SubmitField("Submit")
I noticed two things in your code:
# this
<a href={{url_for('profile')}}>
# should be
<a href={{url_for('profile', id=contact.id)}}>
# otherwise Flask can't find the route, because it needs an id
And the other one:
{% for detail in Contacts %}
There is no such thing as a Contacts variable in your template, because your view function does not send it. Just get rid of the loop and use detail directly, because it's what you sent to the template.
I'm new in django, I would like to display my view.py to index.html,
the view.py:
def index(request):
context_dict = {}
customers = Customer.objects.all()
carts = Cart.objects.select_related('customer')
goods = Good.objects.select_related('cart__customer')
context_dict['Cart']=carts
context_dict['Good']=goods
context_dict['Customer'] = customers
return render(request, 'index.html', context=context_dict)
and the index.html for loop is like this:
<ul>
{% for customer in Customer %}
<li>{{ customer.name }}</li>
{% for cart in Cart %}
{% if customer.id == cart.id %}
{% for good in Good %}
{% if cart.id == good.id %}
{{good.name}}--{{good.count}}--{{good.price}}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
but the result displayed like this:
Bob should have water--2--50 under it.
it seems customer.id == cart.id cannot match.
but I don't know how to fix it.
please help, thanks a lot!
customer.id should match card.customer_id and so on (cart.good_id == good.id)
if you have a single cart with a single good in it your solution will still return all of the customers, all of the goods to show a single line on the page - this is not really a good solution.
So try using joins and retrieve only needed data, e.g.:
actual_carts = Cart.objects.all().select_related('customer', 'good').order_by('customer_id', )
This will return only customers and goods mentioned in carts. select_related will let you access all the needed info from customers as well as from goods.
What am i doing wrong? I just want the username of the user. I am logged so i don't know why it's not working.
views.py
from django.contrib.auth.models import User
def check_user(request):
data = dict()
user = User.objects.all()
data['user'] = user
return render(request, 'check_user.html', data)
urls.py
url(r'^check_user/$', views.check_user, name='check_user'),
check_user.html
{{ request.user.is_authenticated }}
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
And i'm getting this:
CallableBool(True)
Welcome, new user. Please log in.
when i should get:
Welcome, admin. Thanks for logging in.
If i use: {% if request.user.is_authenticated %} instead of {% if user.is_authenticated %} i'm getting this:
Welcome, . Thanks for logging in.
Your problem is in your view, you don't need to create data['user']. Because in template, Django already pass the template variable {{ user }}.
So in your view remove this data dict, and keep the current context, without modification.
def check_user(request):
return render(request, 'check_user.html', {})
And your template will work. (don't need to change something)
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates
user is queryset object in your template, in other words it is list of users, not single user.
If you need current user use this:
{% if request.user.is_authenticated %}
<p>Welcome, {{ request.user.username }}. Thanks for logging in.</p>
instead of
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
UPDATE
As Wilfried said in his answer and comment, it is not good practice to override variable user in template. So if you need list of users in view it would be better to rename context variable to users:
users = User.objects.all()
data['users'] = users
Please, I am creating a web app, when a user log in, he/she can see other users, and can click on the user's username to view that user's profile. But I am finding it difficult to display other users profile except the currently logged in user. Kindly help me on how to go about this. I would prefer a function based view.
views
def profile(request, username):
context = {
'userprofile': User.objects.get(username=username),
}
return render_to_response('profile.html',context)
profile.html
Name: {{ userprofile.get_full_name }}
Username: {{ userprofile.username }}
urls
url(r'^/profile/(?P<username>\w+)/$', auth(profile), {}, name='chat_history')
what I don't know is how apply this url on a username. As in this:
home.html
{% if messages %}
{% for message in messages %}
{% if message.sender == user %}
<p> {{ message.sender }} >
{{ message.receiver }} : <br/>
{{ message.message }}
<sub>{{ message.creation_date }}</sub>
</p>
{% endif %}
{% endfor %}
{% else %}
{% trans 'No chat history of you and this user' %}
{% endif %}
Have got no idea what 'xxxxx' should be
Probably importing User model from django.contrib.auth and passing it to your views could help.
I have done this tutorial as it explained here:
and here
but there is some points that i can not understand.
When i log in using a facebook account i can get access also easily to my admin page and i want it to be happened (because it is not secure), so is there a way to fix that ?
If i want to bring that registred user to another template, i can do it only whith direct_to_template method in my url dispatcher, here is an example:
url(r'^tags$', direct_to_template, {'template' : 'user.html' }),
is there another way to do it.
Finally to be more clear, here is some snippets of my project:
urls.py
urlpatterns = patterns('',
#All Auth URLS
url(r'^accounts/', include('allauth.urls')),
url(r'^accounts/profile/', direct_to_template, { 'template' : 'profile.html' }),
#nav urls
url(r'^$','fb.views.home', name="home"),
url(r'^tags$', direct_to_template, {'template' : 'tags.html' }),
views.py
def home(request):
return render_to_response("base.html", locals(), RequestContext(Context))
base.html
.....
{% block body %}
{% block content %}
{% if user.is_authenticated %}
{{ user.username }} <p> you are logged in </p>
<p><a href="/accounts/logout/" >Logout </a></p>
{% else %}
<p> you are not authenticated : </p>
<a href="/accounts/facebook/login/" >Login with Facebook </a>
{% endif %}
{% endblock content %}
{% endblock body %}
...
profile.html
...
{% block content %}
{% if user %}
<h1>Welcome, {{user.first_name}}</h1>
<p>Following is the Extra information that facebook has provided to allauth:</p>
{% for account in user.socialaccount_set.all %}
<p>First Name: {{ account.extra_data.first_name }}</p>
<p>Last Name: {{ account.extra_data.last_name }}</p>
<p>Profile Link: {{ account.extra_data.link }}</p>
{% endfor %}
{% endif %}
Go to tags
{% endblock content %}
{% endblock body %}
tags.html
{{user.socialaccount_set.all.0.get_avatar_url}} <br/>
{{user.socialaccount_set.all.0.uid}} <br/>
{{user.socialaccount_set.all.0.get_provider_account }} <br/>
Finally thanks in advance for your help .
The secrect is:
After logging in, the user was hiding in the request object, so view shoukd be like this:
def home(request):
user = request.user
return render_to_response("base.html", locals(), RequestContext(Context))
Now, the problem is resolved, but i wonder why the Context User object was anounymous.
i am asking this question because that last user object woiuld have the same value of the request user in a simple authentication