I am using django-allauth for my authentication. I have a dashboard which has many links.
user.html
`{% include 'sidebar.html' %}
<h1>Profile View</h1>
<p>{{ profile.username }}</p>`
change_password.html
`{% include 'sidebar.html' %}
<h2>Change Password</h2>
<p>{{ profile.username }}</p>`
sidebar.html
`Profile View
Change Password`
views.py
class ProfileView(DetailView):
template_name = "user.html"
queryset = User.objects.all()
context_object_name = 'profile'
slug_field = "username"
change password view is from django-allauth. How can i pass the the username from the ProfileView to the change password view so that i can display in the change_password.html page.
Main Problem
Since i have been including sidebar.html to both the views, it works good in ProfileView but when i navigate to change_password view i get the following error
Reverse for 'profile_view' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['(?P[-\w.#+-]+)/$']
the error does not show in ProfileView because the profile.username returns a value, but when i navigate to change_password view i get the above error because the profile context object only passes in that particular view. i want to use that context_object through out the project.
Is there a simple way to do it? other than build a JSON API?
I just had a quick look at the template tags provided by the allauth app. I haven't tested this (nor do I use the project) but this looks like what you are looking for.
In your template, any template...
{% load account %} {# this should be near the top with any other load tags #}
{# this can be anywhere in the template. I'm not positive what it returns but it looks like it is the username. #}
<p>
{% user_display user %}
</p>
I found the answer here.
I got it working, Thank you #teewuane
sidebar.html
Profile View
replaced as
Profile View
Related
I have taken a hint from this post
Customising tags in Django to filter posts in Post model
I have created the template tag but I am not sure how to use it in my html. I have a home.html where I want to show three featured post. I am looking for something like {% for post in featured_post %} and then show the post detail.
Also, do I necessarily need to create a featured_posts.html as in the above post because I don't want any extra page for the featured post. I just want them to add on my home page in addition to other stuff.
What I am trying to do is I have created a template tag as under
from django import template
register = template.Library()
#register.inclusion_tag('featured_posts.html')
def featured_posts(count=3):
if Post.is_featured:
featured_posts = Post.published.order_by('-publish')[:count]
return {'featured_posts': featured_posts}
The problem I am facing here is I can't import the Post model from model. My directory structure is somewhat like this:-
I have an app named posts.
Inside that I have models.py and templatetags module and inside the template tag I have blog_tags.py
I couldn't do the relative import.
And then created a new page featured_posts.html as under:-
<ul>
{% for post in featured_posts %}
<li>{{ post.title }} </li>
{% endfor %}
</ul>
Now, I want to use it in my home.html. How can I use it?
Edit:- As mentioned above I could load the models as under:-
from posts.models import Post
home.html
{% load blog_tags %}
{% featured_posts %}
Call your tag. That's it.
or
{% featured_posts count=15 %}
Note, featured_posts here is not the post list (which is iterated in for loop) from context but function name: def featured_posts(count=3). They have the same name in your code and probably this has confused you a little.
I have started learning django, I'm not sure how generic view works. I read django documentation several times, I can't gain clear understanding of how generic view works. Very strange but it works well. It retrieves data from the database and renders data on the browser.
Here is snippet code of polls/urls.py.
url(r'^$', views.IndexView.as_view(), name = 'index')
It will go to the IndexView class in views.py.
Here is snippet code of polls/views.py.
from django.views import generic
from .models import Question, Choice
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:5]
When I change template_name as something, the exception has occurred as follows.
TemplateDoesNotExist: polls/question_list.html
What does question_list.html mean?
Where does it come from?
And here is index.html.
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>
{{ question.question_text }}
</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
As you can see, index.html file use latest_question_list as a parameter but I'm not sure how can the latest_question_list be used.
In views.py file, what does context_object_name = 'latest_question_list' mean?
Where does the 'latest_question_list' come from and how can index.html use latest_question_list?
Do I use context_object_name in views.py?
What's the role of the get_queryset() function?
What does question_list.html mean?
It means that there is a file inside polls dir like index.html
if you want to have a file with a diff name you have to change the name of template_name='polls/name_of_file_inside_polls'
Do I use context_object_name in views.py?
The context is the place where you put some vars to use it in your templates, that's why you can use latest_question_list
What's the role of the get_queryset() function?
It creates the query that is passed to the template.
I understand what they mean.
By default, the DetailView generic view uses a template called /_detail.html. In my case, it would use the template "polls/question_detail.html". The template_name attribute is used to tell Django to use a specific template name instead of the autogenerated default template name. I also specify the template_name for the results list view – this ensures that the results view and the detail view have a different appearance when rendered, even though they’re both a DetailView behind the scenes.
For DetailView the question variable is provided automatically – since we’re using a Django model (Question), Django is able to determine an appropriate name for the context variable. However, for ListView, the automatically generated context variable is question_list. To override this we provide the context_object_name attribute, specifying that we want to use latest_question_list instead.
I've been trying to figure this out for a while now but I feel like I don't know the framework well enough to debug this myself.
Basically I'm creating a little blog style site and I'm trying to create a list of posts which can link to the page to read the post itself.
I have a for loop in my template:
templates/home.py
<h1>Home Page</h1>
<p>welcome to the ven home page, {{ username }}!</p>
Click here to log out
<br>
Click here to create a post
<h2>Posts:</h2>
{% for post in posts %}
<div>
<hr>
<h4>{{post.title}}</h4>
<p>{{post.body}}</p>
<p><i>{{post.tags}}</i></p>
</div>
{% endfor%}
It's the line <h4>{{post.title}}</h4> which is causing the problem. I'm getting the error
Reverse for 'show' with keyword arguments '{'id': 1}' not found. 1
pattern(s) tried: ['posts/(?P<post_id>\\d+)/view/$']
here is my urls file
url(r'^$', views.CreateFormView.as_view(), name='create'),
url(r'^(?P<post_id>\d+)/view/$', views.show_post, name='show')
The create method link works fine
and here is the view which loads the template:
def home(request):
if not request.user.is_authenticated:
return redirect('users:login')
posts = Post.objects.all()
username = request.user.username
return render(request, 'ven/home.html', {'username': username, 'posts':
posts})
If more information is needed then let me know and I will provide it.
All other answers have said that this error is to do with the namespace, but it's working fine with the create link so I'm stumped.
Thanks in advance!
The argument names are mismatching.
You'd want to change <h4>{{post.title}}</h4>
to
<h4>{{post.title}}</h4>
Since in urls.py the show url is defined as '^(?P<post_id>\d+)/view/$'.
I am trying to login the user into his profile and need to pass the username to a navbar in a base template.
I have base.html(base template) and index-client.html(body template).
Now from login view, i would like to pass username value to base.html.
Code after user authentication is:
if user is not None:
if user.is_active:
login(request, user)
return redirect('index-client.html')
I need to pass username to base.html i.e. a base template.
The {{ request.user }} method is built in with Django, and you can use this in any template to access the User object.
If you want to display the user's name in the navbar you can use:
{{ request.user.first_name|capfirst }} {{ request.user.last_name|capfirst }}
As can be seen this has the use of a template tag |capfirst to capitalise the first letter.
according to doc, redirect will accept
A model: the model’s get_absolute_url() function will be called.
A view name, possibly with arguments: reverse() will be used to reverse-resolve the name.
An absolute or relative URL, which will be used as-is for the redirect location.
So redirect to specific view which render the base html. In html
{% if request.user.is_authenticated %}
{{ request.user.username }}
{% endif %}
I used django-registration app. Everything goes fine. User gets registered with email verification but when user logs in and is redirected to mainpage the auth template tags such as {% if user.is_authenticated %} returns false.
I am having this in my login.html
<input type="hidden" name="next" value="/" />
After login I want to redirect the user to main page and in mainpage.html
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
but user.is_authenticated returns false here. What might be the problem here? Thanks
Try using {% if request.user.is_authenticated %} instead. It's entirely possible that user isn't being passed into the context dictionary within the view. If the object is not found within the template, it will just skip to the else portion of the block. Rendering templates are strange in django, as what would normally be an exception, is swallowed.
Can you show us the code for the view which handles the URL '/'? I think this is where your problem lies, rather than in your use of django-registration.
Does this view put user in the context? If you want to use it in the template, you're going to have to put it in. If you want to use request in the context, then you need to make sure that you pass an instance of RequestContext as the context rather than just a standard context object.
Here's what worked for me.
Suppose your main page gets processed by a view in views.py
from django.shortcuts import render_to_response
def mainpage(request):
return render_to_response('mainpage.html')
You need to add RequestContext to include to user object. Change your view to
from django.shortcuts import render_to_response
from django.template import RequestContext
def mainpage(request):
return render_to_response('mainpage.html', context_instance=RequestContext(request))