2016-01-16 180 views
1

免責聲明:我是Python和Django的一個月,並構建我的第一個項目作爲新手程序員。登錄返回擴展模型而不是基本模型

所需功能:新用戶打開應用程序並註冊用戶名和密碼。然後他們被帶到一個資料頁面,在那裏他們輸入一系列的東西。他們提交它,現在他們應該鏈接到一個用戶(創建者)和一個配置文件(內容)。他們只能看到和編輯他們的頁面。

當前問題:當我以管理員身份登錄時,可以看到所有配置文件信息。當我以普通用戶身份登錄時,我只能看到該用戶在註冊(用戶名)時所要求的信息。我沒有看到任何配置文件信息,雖然字段(空)在那裏。實際上,登錄將我帶到用戶,而不是簡介。我猜我的模型有問題。我是否應該從AbstractUser模型開始,然後使用擴展Profile模型的One-To-One?我想我可能已經做了這個倒退。謝謝!!

我看到的,當:

已登錄爲用戶 「bbasil」:

學生姓名:

首選名字:
電子郵件:[email protected]
學校名稱:
短生物:
興趣愛好:,

已登錄爲 「管理員」:

學生姓名:布蘭奇卡米爾羅勒

首選名字:貝蒂
電子郵件:[email protected]
學校名稱:狼吞虎嚥大學
個人簡介:我喜歡製作冰雕,烤長棍麪包和莎莎舞。
興趣:雕塑,烤麪包,薩爾薩舞

MODELS.PY

from django.core.validators import RegexValidator 
from django.template.defaultfilters import slugify 
from django.template import Template, Context 
from utils import get_domain 
from django.contrib.auth.models import User 
from 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 to work for domain logic 
     t = Template('{{ first_name|slice:"1" }}') 
     c = Context ({'first_name': first_name }) 
     return t.render(c) 

    @property # what does this do? 
    def name(self): 
     return "%s %s" % (self.first_name, self.last_name) 


    def save(self, *args, **kwargs): 
     if not self.webcode: # What are 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 
from django.template.defaultfilters import slugify 
from django.shortcuts import render, redirect 
from student_profile.forms import ProfileForm, UploadForm 
from 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, 
    }) 

註冊和註冊頁面
兩者都是從Django的俗套:

registration_form

{% extends 'layouts/base.html' %} 

{% block title %}Registration Form - {{ block.super }}{% endblock %} 

{% block content %} 

<h1>Register an 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 a Profile - {{ block.super }}{% endblock %} 

{% block content %} 

<h1>Create Your Profile</h1> 

<form role="form" action="" method="post"> 
{% csrf_token %} 
{{ form.as_p }} 
<input type="submit" value="Submit" /> 
</form> 

{% endblock %} 
+0

Django的用戶類有一些你在配置文件中使用的字段...試着利用它。問題可能出現在您的views.py文件中。你能顯示你的視圖代碼嗎? –

+0

您能否包含與註冊和註冊頁面相關的代碼? – Sayse

回答

0

事實證明,我不應該登錄到管理和前端使用相同的瀏覽器。我通過使用隱身模式登錄管理員來解決此問題,以便管理員登錄不會覆蓋當前用戶的會話。

當我發現這一點時,我感到有點無聊,但可能會幫助另一個新的人Django!