Thursday 15 May 2014

python - create profiles simultaneously with registration -



python - create profiles simultaneously with registration -

i have user registration scheme on project , profile system, problem when create user registration don't create profile simultaneously , reason when create new user , seek come in own profile, obtain error:

user has no userprofile.

this configuration:

views.py:

from django.shortcuts import render_to_response django.template import requestcontext dracoin.apps.synopticup.models import card dracoin.apps.home.forms import contactform,loginform,registerform,userprofileform django.core.mail import emailmultialternatives django.contrib.auth.models import user dracoin.settings import url_login django.contrib.auth import login,logout,authenticate django.http import httpresponseredirect django.core.paginator import paginator, emptypage, invalidpage django.contrib.auth.decorators import login_required def index(request): homecoming render_to_response('home/index.html',context_instance=requestcontext(request)) @login_required(login_url=url_login) def landing(request): homecoming render_to_response('home/landing.html',context_instance=requestcontext(request)) @login_required(login_url=url_login) def shop(request,pagina): lista_tarj = card.objects.filter(status=true) paginator = paginator(lista_tarj,5) try: page = int(pagina) except: page = 1 try: tarjetas = paginator.page(page) except (emptypage,invalidpage): tarjetas = paginator.page(paginator.num_pages) ctx = {'tarjetas':tarjetas} homecoming render_to_response('home/shop.html',ctx,context_instance=requestcontext(request)) @login_required(login_url=url_login) def singlecard(request,id_tarj): tarj = card.objects.get(id=id_tarj) ctx = {'card':tarj} homecoming render_to_response('home/singlecard.html',ctx,context_instance=requestcontext(request)) @login_required(login_url=url_login) def contacto(request): info_enviado = false # define si se envio la informacion o no email = "" titulo = "" texto = "" if request.method == "post": formulario = contactform(request.post) if formulario.is_valid(): info_enviado = true email = formulario.cleaned_data['email'] titulo = formulario.cleaned_data['titulo'] texto = formulario.cleaned_data['texto'] # configuracion de enviado de correos vis hotmail to_supp = 'elzipa25@gmail.com' html_content = "informacion recibida<br><br><br>***mensaje***<br><h3>%s<h3><br><br>%s<br><br>%s"%(titulo,email,texto) msg = emailmultialternatives('correo de contacto',html_content,'from@server.com',[to_supp]) msg.attach_alternative(html_content,'text/html') # contenido definido como html msg.send() else: formulario = contactform() ctx = {'form':formulario,'email':email, 'titulo':titulo, 'texto':texto, 'info_enviado':info_enviado} homecoming render_to_response('home/contacto.html',ctx,context_instance=requestcontext(request)) def login_view(request): mensaje = "" if request.user.is_authenticated(): homecoming httpresponseredirect('/') else: if request.method == "post": form = loginform(request.post) if form.is_valid(): next = request.post['next'] username = form.cleaned_data['username'] password = form.cleaned_data['password'] usuario = authenticate(username=username,password=password) if usuario not none , usuario.is_active: login(request,usuario) homecoming httpresponseredirect(next) else: mensaje = "user or password aren't correct" next = request.request.get('next') form = loginform() ctx = {'form':form,'mensaje':mensaje,'next':next} homecoming render_to_response('home/login.html',ctx,context_instance=requestcontext(request)) def logout_view(request): logout(request) homecoming httpresponseredirect('/') def register_view(request): form = registerform() if request.method == "post": form = registerform(request.post) if form.is_valid(): first_name = form.cleaned_data['first_name'] usuario = form.cleaned_data['username'] email = form.cleaned_data['email'] password_one = form.cleaned_data['password_one'] password_two = form.cleaned_data['password_two'] u = user.objects.create_user(first_name=first_name,username=usuario,email=email,password=password_one) u.save() homecoming render_to_response('home/thanks_register.html',context_instance=requestcontext(request)) else: ctx = {'form':form} homecoming render_to_response('home/register.html',ctx,context_instance=requestcontext(request)) ctx = {'form':form} homecoming render_to_response('home/register.html',ctx,context_instance=requestcontext(request)) def edit_profile(request): user = request.user user_profile = user.userprofile if request.method == 'post': user_profile_form = userprofileform(request.post) if user_profile_form.is_valid(): #update user profile user_profile.name = request.post['name'] user_profile.user = user user_profile.email = request.post['email'] user_profile.save() else: user_profile_form = userprofileform(instance=user_profile) variables = requestcontext( request, { 'user_profile_form': user_profile_form} ) homecoming render_to_response( 'home/edit_profile.html', variables )

forms.py:

from django import forms django.contrib.auth.models import user dracoin.apps.home.models import userprofile class contactform(forms.form): email = forms.emailfield(widget=forms.textinput()) titulo = forms.charfield(widget=forms.textinput()) texto = forms.charfield(widget=forms.textarea()) class loginform(forms.form): username = forms.charfield(widget=forms.textinput()) password = forms.charfield(widget=forms.passwordinput(render_value=false)) class registerform(forms.form): username = forms.charfield(label="nombre de usuario",widget=forms.textinput()) first_name = forms.charfield(label="telefono",widget=forms.textinput()) email = forms.emailfield(label="correo electronico",widget=forms.textinput()) password_one = forms.charfield(label="password",widget=forms.passwordinput(render_value=false)) password_two = forms.charfield(label="confirmar password",widget=forms.passwordinput(render_value=false)) def clean_username(self): username = self.cleaned_data['username'] try: u = user.objects.get(username=username) except user.doesnotexist: homecoming username raise forms.validationerror('nombre de usuario ya existe') def clean_email(self): email = self.cleaned_data['email'] try: u = user.objects.get(email=email) except user.doesnotexist: homecoming email raise forms.validationerror('email ya registrado') def clean_password_two(self): password_one= self.cleaned_data['password_one'] password_two= self.cleaned_data['password_two'] if password_one == password_two: pass else: raise forms.validationerror('passwords no coincidentes') class userprofileform(forms.modelform): class meta: model = userprofile

models.py:

rom django.db import models django.contrib.auth.models import user def url(self,filename): ruta = "multimediadata/users/%s/%s"%(self.user.username,filename) homecoming ruta class userprofile(models.model): name = models.charfield(max_length=30, default='') user = models.onetoonefield(user) photo = models.imagefield(upload_to=url) email = models.emailfield(max_length=75) def __unicode__(self): homecoming self.user.username

and urls.py:

from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^$','dracoin.apps.home.views.index' ,name='vista_principal'), url(r'^landing/$','dracoin.apps.home.views.landing' ,name='vista_aterrizaje'), url(r'^shop/page/(?p<pagina>.*)/$','dracoin.apps.home.views.shop' ,name='vista_tienda'), url(r'^card/(?p<id_tarj>.*)/$','dracoin.apps.home.views.singlecard',name='vista_single_card'), url(r'^contacto/$','dracoin.apps.home.views.contacto' ,name='vista_contacto'), url(r'^login/$','dracoin.apps.home.views.login_view',name='vista_login'), url(r'^logout/$','dracoin.apps.home.views.logout_view',name='vista_logout'), url(r'^registro/$','dracoin.apps.home.views.register_view',name='vista_registro'), url(r'^edit_profile/$','dracoin.apps.home.views.edit_profile', name='vista_profile'),

i can come in , edit profiles user inteface if created profile admin panel

my questions is; should alter in configuration permit me create new profiles simultaneously while register new users user interface?.

i know can create profiles registrations other metods "abstractuser" prefer extend "user" preserve much possible actual structure.

thank you!!!

change photo field in userprofile this:

class userprofile(models.model): name = models.charfield(max_length=30, default='') user = models.onetoonefield(user) photo = models.imagefield(blank=true,upload_to=url) email = models.emailfield(max_length=75) def __unicode__(self): homecoming self.user.username

this allow add together userprofile without photo.

simply add together userprofile when create new user this:

def register_view(request): form = registerform() if request.method == "post": form = registerform(request.post) if form.is_valid(): first_name = form.cleaned_data['first_name'] usuario = form.cleaned_data['username'] email = form.cleaned_data['email'] password_one = form.cleaned_data['password_one'] password_two = form.cleaned_data['password_two'] u = user.objects.create_user(first_name=first_name,username=usuario,email=email,password=password_one) u.save() #add user profile user_profile = userprofile(name=first_name,user=u,email=email) user_profile.save() homecoming render_to_response('home/thanks_register.html',context_instance=requestcontext(request)) else: ctx = {'form':form} homecoming render_to_response('home/register.html',ctx,context_instance=requestcontext(request)) ctx = {'form':form} homecoming render_to_response('home/register.html',ctx,context_instance=requestcontext(request))

python django django-registration user-profile

No comments:

Post a Comment