python - Login returns extended model not the base model -


disclaimer: i'm 1 month python , django, , building first project budding programmer.

desired functionality: new user opens app , registers username , password. taken profile page input series of things. submit it, , should linked user (creator) , profile (content). can see , edit page.

current problem: when log in admin, can see profile information. when log in regular user, see information requested @ registration (username) user. not see profile information, although fields(empty) there. effectively, login taking me user not profile. i'm guessing have problems in models. should start on abstractuser model , use one-to-one extended profile model? think may have done backwards. thank you!!

what see when:

logged in user "bbasil":

student name:

preferred first name:
email: bbasil@gobble.edu
school name:
short bio:
interests: , ,

logged in "admin":

student name: blanche camille basil

preferred first name: betty
email: bbasil@gobble.edu
school name: gobble university
short bio: make ice sculptures, bake baguettes, , salsa dance.
interests: sculpture, baking bread, salsa dancing

models.py

from django.core.validators import regexvalidator django.template.defaultfilters import slugify django.template import template, context utils import get_domain django.contrib.auth.models import user django.db import models  class timestamp(models.model):     created = models.datetimefield(auto_now_add=true)     updated = models.datetimefield(auto_now=true)      class meta:         abstract = true  alphanumeric = regexvalidator(r'^[0-9a-za-z]*$', 'alphanumeric characters only.')  class profile(timestamp):     first_name = models.charfield(max_length=100, blank=true, null=true, validators=[alphanumeric])     middle_name = models.charfield(max_length=100, blank=true, null=true, validators=[alphanumeric])     last_name = models.charfield(max_length=100, blank=true, null=true, validators=[alphanumeric])     preferred_name = models.charfield(max_length=100, blank=true, null=true, validators=[alphanumeric])     name = models.charfield(max_length=100)     email = models.emailfield(max_length=254)     school_name = models.charfield(max_length=100)     short_bio = models.textfield()     interest1 = models.charfield(max_length=100)     interest2 = models.charfield(max_length=100)     interest3 = models.charfield(max_length=100)     slug = models.slugfield(unique=true)     domain = models.charfield(max_length=100)     webcode = models.charfield(max_length=100)     user = models.onetoonefield(user, blank=true, null=true)      def middle_initial(self): # doesn't seem work domain logic         t = template('{{ first_name|slice:"1" }}')         c = context ({'first_name': first_name })         return t.render(c)      @property # do?     def name(self):         return "%s %s" % (self.first_name, self.last_name)       def save(self, *args, **kwargs):         if not self.webcode: # these ifs saying?             self.webcode = user.objects.make_random_password(length=10)         if not self.slug:             self.slug = slugify(self.name)         if not self.domain:             self.domain = get_domain(first_name=self.first_name, middle_name=self.middle_name, last_name=self.last_name, preferred_name=self.preferred_name)         return super(profile, self).save(*args, **kwargs)      def __str__(self):         return self.name   def get_image_path(instance, filename):     return '/'.join(['profile_images', instance.profile.slug, filename])   class user(models.model):     profile = models.onetoonefield(profile, blank=true, null=true, related_name="creators")   class upload(timestamp):     profile = models.foreignkey(profile, related_name="uploads")     image = models.imagefield(upload_to=get_image_path) 

views.py

from django.conf import settings django.template.defaultfilters import slugify django.shortcuts import render, redirect student_profile.forms import profileform, uploadform student_profile.models import profile, upload   def index(request):     if not request.user.is_authenticated():         return redirect('%s?next=%s' % (settings.login_url, request.path))     else:         if request.user.is_superuser:                 profiles = profile.objects.all().order_by('last_name')             return render(request, 'index.html', {                 'profiles': profiles,                 })         else:             profile = request.user             return render(request, 'profiles/profile_detail.html', {                 'profile': profile,             })  def profile_detail(request, slug):     profile = profile.objects.get(slug=slug)     uploads = profile.uploads.all()      return render(request, 'profiles/profile_detail.html', {         'profile': profile,         'uploads': uploads,     })  def edit_profile(request, slug):     profile = profile.objects.get(slug=slug)     form_class = profileform     if request.method == 'post':         form = form_class(data=request.post, instance=profile)         if form.is_valid():             form.save()             return redirect('profile_detail', slug=profile.slug)     else:         form = form_class(instance=profile)         return render(request, 'profiles/edit_profile.html', {         'profile': profile,         'form': form,     })  def create_profile(request):     form_class = profileform     if request.method == 'post':         form = form_class(request.post)         if form.is_valid():             profile = form.save(commit=false)             profile.user = request.user             profile.slug = slugify(profile.name)             profile.save()             return redirect('profile_detail', slug=profile.slug)     else:         form = form_class()         return render(request, 'profiles/create_profile.html', {         'form': form,      })  def edit_profile_uploads(request, slug):     profile = profile.objects.get(slug=slug)     form_class = uploadform      if request.method == 'post':         form = forn_class(data=request.post, files=request.files, instance=profile)         if form.is_valid():             upload.objects.create(                 image=form.cleand_data['image'],                 profile=profile,             )              return redirect('edit_profile_uploads', slug=profile.slug)      else:         form = form_class(instance=profile)         uploads = profile.uploads.all()         return render(request, 'profiles/edit_profile_uploads.html', {         'profile': profile,         'form': form,         'uploads': uploads,     }) 

registration , signup pages
both cookie cutter django:

registration_form

{% extends 'layouts/base.html' %}  {% block title %}registration form - {{ block.super }}{% endblock %}  {% block content %}  <h1>register account</h1>  <form role="form" action="" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="submit" />   </form>  {% endblock %} 

create_profile

{% extends 'layouts/base.html' %}  {% block title %}create profile - {{ block.super }}{% endblock %}  {% block content %}  <h1>create profile</h1>  <form role="form" action="" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="submit" />   </form>  {% endblock %} 

it turns out should not have logged admin , front end same browser. fixed logging admin using incognito mode admin login doesn't override current user's session.

i felt little silly when discovered this, might person new django!


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -