Friday, September 27, 2013

Re: example for project with user login

You can watch my code: 


I am using django-registration with templates.

And django 1.4 .. i have to update the code to django 1.5 but it is the same idea.



Jesús Lucas Flores | Sysadmin, Web Developer and IT Startups Lover

 es.linkedin.com/in/jesuslucas
 github.com/jelukas
 @jelukas89 
 Skype: jelukas89




2013/9/27 Rafael E. Ferrero <rafael.ferrero@gmail.com>
in settings.py
in TEMPLATE_DIRS put all your templates folders where django go to find them

in MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
 )

in INSTALLED_APPS = (
    'django.contrib.auth',  #important to login and logout
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'YourProject.YourApp',
)

and create this
LOGIN_URL = '/url/to/login/'
LOGOUT_URL = '/url/to/logout/'
LOGIN_REDIRECT_URL = '/url/to/your/view'


in your general urls.py

from django.contrib.auth.views import login, logout_then_login
from django.conf.urls.defaults import patterns, url, include
from django.conf import settings
 
from django.contrib import admin
admin.autodiscover()
 
urlpatterns = patterns('',
    (r'^YourApp/', include('YourProject.YourApp.urls')),
    (r'^login/$', login, {'template_name': 'login.djhtml'}),
    (r'^logout/$', logout_then_login),
    # For admin
    url(r'^admin/', include(admin.site.urls)),
)
 
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )



in YourApp/urls.py
from django.conf.urls.defaults import patterns, url
 
urlpatterns = patterns('YourProject.YourApp.views',
    url(r'^$', 'YourViewName'),
)



need a base.djhtml in YourTemplateFolder
in YourTemplateFolder/login.djhtml

{% extends "base.djhtml" %}
{% block cuerpo %}
    {% if form.errors %}
       <p>Su nombre de usuario y contraseña no coinciden. Por favor intente nuevamente.</p>
    {% endif %}       
    <form method="post" action="">
    {% csrf_token %}               
        <label>Nombre de Usuario</label><input id="id_username" type="text" name="username" maxlength="30" /><br />
        <label>Clave de Usuario</label><input type="password" name="password" id="id_password" /><br />               
        <input type="submit" value="login" />
        <input type="hidden" name="next" value="{{ next }}" />
    </form>
{% endblock %}




in your YourApp/views.py

from django.contrib.auth.decorators import login_required
from django.template.context import RequestContext

@login_required
def yourViewName(Request):
    #your code here
    return render_to_response('yourTemplate', {'YourDict': yourDict},
                              context_instance=RequestContext(Request))




I think that something like that will work, its copy of a small project mine in django 1.3.1, its admin somewher ther but you can eliminate. (in www.fsorgosuardi.com.ar/dj/login its this code running... to see the site just go to www.fsorgosuardi.com.ar)

you can, later, try with allauth app... its really cool stuff



2013/9/27 Joachim Wuttke <j1wuttke@gmail.com>
With user login to the user views.
I would like to build my application by incrementally modifying a working small application
that is just a little more complete than the poll application from the basic tutorial.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment