Problem with user.username

28 views
Skip to first unread message

Suprnaturall

unread,
Jul 12, 2011, 6:31:27 AM7/12/11
to Django users
Hi,

First of all, apologize me for my poor, poor english.

I m new with django and i m stuck with a odd thing.

I created a login form and use the default django login handlet like
this :

base.html :
(...) {% if user.is_authenticated %}
<a href="#">{{user.username }}</a>
{% else %}
<a href="{% url django.contrib.auth.views.login %}?
next={{request.path}}">Login</a>
{% endif %}
<a href="/accounts/logout/">log out</a
(...)
login.html :
{% extends "base.html" %}

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action='.'>
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value={{request.path}} />
</form>
{% endblock %}


The problem is : when i m in login.html i see my user.username, but
when i submit the login form and being redirect to index i don t
retrieve the user.username.
I add in my views.py this import :
from django.template import Context, loader, RequestContext
from empireFront.models import Video
from django.http import HttpResponse
from django.contrib import auth
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.contrib.sessions.models import Session

I heard something about use render_to_response but when, where and
how ?

Thx for your help !



Kenneth Gonsalves

unread,
Jul 12, 2011, 10:33:22 AM7/12/11
to django...@googlegroups.com
On Mon, 2011-07-11 at 23:31 -0700, Suprnaturall wrote:
> The problem is : when i m in login.html i see my user.username, but
> when i submit the login form and being redirect to index i don t
> retrieve the user.username.

do you have this in your settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
...
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

nicolas HERSOG

unread,
Jul 12, 2011, 12:41:45 PM7/12/11
to django...@googlegroups.com
Hi !

It's very strange, i just found a way to solve my probleme but i don't have 'django.core.context_processors.request' in my TEMPLATE_CONTEXT_PROCESSORS.

For solve my problem i added in my views.py :
from django.shortcuts import render_to_response

this is my previous index views who didn t work :

 def index(request):
     videos = Video.objects.all()
     t = loader.get_template('myFront/index.html')
     c = Context({
         'datas': datas,
     })
     return HttpResponse(t.render(c))

and this is the solving one :
 def index(request):
     videos = Video.objects.all()
     t = loader.get_template('myFront/index.html')
     c = Context({
         'datas': datas,
     })

     return render_to_response('empireFront/index.html', c, context_instance=RequestContext(request))

Is a a correct way to do in Django ?

I'ill try your solution too,

Thx :)






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Tom Evans

unread,
Jul 12, 2011, 1:07:38 PM7/12/11
to django...@googlegroups.com

Programming by permutation is never a good strategy.
render_to_response() takes 4 arguments[1]

render_to_response(template_name[, dictionary][, context_instance][, mimetype])

In your 'working but no idea why' example, you are providing a Context
as the dictionary, which works as contexts are dict-like in their
nature, and a RequestContext (with no values) as the context. The
contents of the supplied dictionary are merged* into the context.

A typical view using RequestContext and render_to_response should look
like this:

def someview(request):
ctxt = RequestContext({
'hello': 'world',
})
return render_to_response('sometemplate.html', context_instance=ctxt)

It is therefore pointless to provide a Context as the dictionary
parameter, and an empty RequestContext as the context parameter.

Cheers

Tom

[1] https://docs.djangoproject.com/en/1.3/topics/http/shortcuts/#render-to-response

* Not really. Contexts actually have a stack of dictionaries, and
works down through the stack to resolve a variable used in a template.
It is easier to think of it as merging them though.

nicolas HERSOG

unread,
Jul 12, 2011, 1:16:58 PM7/12/11
to django...@googlegroups.com
Thx !

I'll read this and try to implement the "right way" :)

thx Tom


--

bruno desthuilliers

unread,
Jul 12, 2011, 1:25:27 PM7/12/11
to Django users
On Jul 12, 3:07 pm, Tom Evans <tevans...@googlemail.com> wrote:
(snip)
> Programming by permutation is never a good strategy.

Indeed. Yet:

> A typical view using RequestContext and render_to_response should look
> like this:
>
> def someview(request):
>   ctxt = RequestContext({
>     'hello': 'world',
>   })
>   return render_to_response('sometemplate.html', context_instance=ctxt)

which is NOT what you'll find in django's doc nor in about 99+% of the
available snippets, pluggable apps and other available source code
examples which all use the same pattern as the OP.

I'm not the last to be somewhat vocal about antipatterns and bad form
and I totally agree on your comment about programming by permutation,
but whether you use one or another (both legal) form to call
render_to_response is a matter of personal preference.

FWIW, I prefer the "canonical" way which let me build my context dict
outside of the call to render_to_response ;)

Tom Evans

unread,
Jul 12, 2011, 1:26:32 PM7/12/11
to django...@googlegroups.com

I did this from memory, and omitted the very important first argument
to RequestContext (doh!). It should look like this:

def someview(request):
ctxt = RequestContext(request, {


'hello': 'world',
})
 return render_to_response('sometemplate.html', context_instance=ctxt)

Cheers

Tom

Tom Evans

unread,
Jul 12, 2011, 1:30:22 PM7/12/11
to django...@googlegroups.com
On Tue, Jul 12, 2011 at 2:25 PM, bruno desthuilliers
<bruno.des...@gmail.com> wrote:
> On Jul 12, 3:07 pm, Tom Evans <tevans...@googlemail.com> wrote:
> (snip)
>> Programming by permutation is never a good strategy.
>
> Indeed. Yet:
>
>> A typical view using RequestContext and render_to_response should look
>> like this:
>>
>> def someview(request):
>>   ctxt = RequestContext({
>>     'hello': 'world',
>>   })
>>   return render_to_response('sometemplate.html', context_instance=ctxt)
>
> which is NOT what you'll find in django's doc nor in about 99+% of the
> available snippets, pluggable apps and other available source code
> examples which all use the same pattern as the OP.
>

Looks exactly like the example from [1], apart from using
render_to_response shortcut rather than manually rendering the
template.

Cheers

Tom

[1] https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext

Bishop Akolgo

unread,
Apr 23, 2020, 3:59:51 PM4/23/20
to Django users
i have a similar problem.  when i installed the env and django it did not require me to create password but after successfully installing django and trying to open admin, i need password and i cannot enter so i am stuck.  Can someone help me?

Jorge Gimeno

unread,
Apr 23, 2020, 4:57:14 PM4/23/20
to django...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7f4f7a9f-cf9a-4bf1-9d63-d2b711a58f68%40googlegroups.com.

Did you create an administrative user?  In Django, it's python manage.py createsuperuser

-Jorge

Allison Tretina

unread,
Apr 23, 2020, 5:04:43 PM4/23/20
to django...@googlegroups.com
Open the python shell in your terminal. 

$ python3 manage.py shell 

Import the modules to access your admin values.

>>> from django.contrib.auth import get_user_model

Get the username first. 

>>> list(get_user_model().objects.fileter(is_superuser=True).values_list('username', flat=True))
['someusername'] # The username attribute is returned as string.

Now, let's try to get the password.

>>> list(get_user_model().objects.fileter(is_superuser=True).values_list('password', flat=True))
['hykeft3_sha365$230001$nUYjkejaijfWESn98jalkoix3rgthaeTklia+SwklksileGs/2jilEFD/kesliI='] # The output is a hex, and other metadata about the password.

Django does not store the raw password. But the password can be set for the admin User object.

>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='someusername')
>>> u= set_password('s0m3pa$$w5rD') 
>>> u.save()
>>> exit()

Return to the admin login and you should be able to login with the new set password. 


Allison Tretina

unread,
Apr 23, 2020, 5:08:53 PM4/23/20
to django...@googlegroups.com
I have two typos on line 6 and 9. My bad.

>>> list(get_user_model().objects.filter(is_superuser=True).values_list('username', flat=True))

>>> list(get_user_model().objects.filter(is_superuser=True).values_list('password', flat=True))


Bishop Akolgo

unread,
Apr 24, 2020, 9:20:09 AM4/24/20
to django...@googlegroups.com
Hi Thks.  Was able to resolve problem after completing migrations and creating the supperuser
regards
bishop

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/b4MdWpPYEW4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJrG93CSfhQwNJ1Lv7XQJcKu%3D-ai8fAwtQv2fRDkQ4X9j9ACfA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages