Sunday 15 March 2015

python - Adding a profile.html page on my project -



python - Adding a profile.html page on my project -

i have set section on website fill form of userprofile called profile.html . have on moment models.py display info on admin panel:

from 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

i want enable possibility of edit info per user on respective profile, thereby can audit user profile info per user in admin panel (localhost:8000/admin) form create respective view , url?

this urls.py (this isn't main urls.py, exceptional urls.py app , lack url profile)

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'),

this models.py profile:

from 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

my views.py (lack userprofile view)

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 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))

thanks!!

def edit_profile(request): user = request.user user_profile = user.userprofile if request.method == 'post': user_profile_form = userprofile(request-post) if user_profile_form.is_valid(): update user profile 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)

try this:

1- add together urls.py:

url(r'^edit_profile/$', 'dracoin.apps.home.views.edit_profile', name='edit_profile'),

2- create form , user_profile view in views.py:

# form class userprofileform(forms.modelform): class meta: model = userprofile #view 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( 'edit_profile.html', variables )

3- add together edit_profile.html template:

<div> <form method="post" action="."> {% csrf_token %} {{ user_profile_form.as_p }} <input type="submit" name="edit_profile" value="done"> </form> </div>

python django django-views user-profile

No comments:

Post a Comment