2016-04-06 29 views
1

嗨,我試圖實現一個分析系統,以便用戶登錄後,學生被識別並從學生模型中提取信息並將其返回到主頁,可能每隔一個系統中的頁面。它也將學生用戶名追加到URL的末尾....希望Python方法的創新,缺少參數

我得到這個錯誤:

profile() missing 1 required positional argument: 'username'

Urls.py

# student urls.py 
    # Import urls and patterns aswell as student views 
    from django.conf.urls import patterns, url 
    from student import views 

    urlpatterns = patterns('', 
    url(r'^(?P<username>[a-zA-Z0-9]+)$', views.index, name='index'), 
    url(r'^$', views.profile, name='profile')) 

Models.py

# student models.py 
    # Import the models db and validators 
    # Also import user information for log in identification 
    from django.db import models 
    from django.core.validators import * 
    from django.contrib.auth.models import User 

    # Specify attributes for the student database 
    class student(models.Model): 
    # Specify choices for years, gender and degree type 
    YEARS = (
    ('1', '1st'), 
    ('2', '2nd'), 
    ('3', '3rd'), 
    ) 
    GENDER = (
    ('M', 'Male'), 
    ('F', 'Female'), 
    ) 
    DEGREE = (
    ('IT', 'Information Technology'), 
    ('CS', 'Computer Science'), 
    ) 

    # Attributes for the student model specified here 
    user = models.OneToOneField(User) 
    student_ID = models.CharField(unique=True, max_length=9, validators=[RegexValidator(regex='^[0-9]{9,9}$', message='Must be 9 unique numbers', code='nomatch')]) 
    first_name = models.CharField(max_length=128) 
    middle_name = models.CharField(max_length=128) 
    last_name = models.CharField(max_length=128) 
    gender = models.CharField(max_length=1, choices=GENDER) 
    year = models.CharField(max_length=1, choices=YEARS) 
    degree = models.CharField(max_length=2, choices=DEGREE) 
    photo = models.ImageField(upload_to="profile_pictures", null=True, blank=True) 

    def __str__(self): 
      return self.student_ID 

Views.py

# student views.py 
    # Import rendering and httpresponse 
    # Also import student model 
    from django.shortcuts import render 
    from django.http import HttpResponse 
    from student.models import student 
    from extra_curricular.models import extra_curricular 
    from module.models import module 
    from skill.models import skill 
    from django.contrib.auth.models import User 

    # Information box displayed on each page 
    def index(request, username): 
     user = User.objects.get(username=username) 
     person = student.objects.get(user=user) 
     return render(request, 'student/home.html', {"person":person}) 

    #Profile page information 
    def profile(request, username): 
     user = User.objects.get(username=username) 
     person = student.objects.get(user=user) 
     experience = extra_curricular.objects.get(user=user) 
     module = module.objects.get(user=user) 
     skill = skill.objects.get(user=user) 
     return render(request, 'student/profile.html', {"person":person}, {"experience":experience}, {"module":module}, {"skill":skill}) 

的Home.html

 {% block content %} 
    <div class = "StudentInfoMain"> 


    </div> 
    <div id="Options"> 
     <ul> 
      <li><a href="/student/">Home</a></li> 
      <li><a href="/extra_curricular/add_extra_curricular/">Add Information</a></li> 
      <li><a href="#">View User Profile</a></li> 
      <li><a href="#">Checklist</a></li> 
      <li><a href="/skill/add_skill/">My Skills</a></li> 
      <li><a href="/alumni/Find_Alumni/">Contact Alumni</a></li> 
     </ul> 
    </div> 
    <div class = "Homebanner"> 
     <p>{{ user.username }}</p> 
     <p>{{ person.student_ID }}</p> 
     <p>{{ person.first_name}}</p> 
     <p>{{ person.last_name }}</p> 
     <p>{{ person.year }}</p> 
     <p>{{ person.degree }}</p> 

    </div> 
    <div id = "Homebody"> 
    <p>Hello {{person.first_name}</p> 
    <p>Welcome to your E-Profile</p> 
    <br> 
    <p>This application is intended as an aid to you during your employment endeavours.</p> 
    <p>It can be used during the process of building a curriculum vitae</p> 
    <p>You may add information of experiences you have had during your educational career</p> 
    <br> 
    <p>For any further information: </p> 
    <br> 


    </div> 


    <footer> 

    </footer> 
    {% endblock %} 

回答

1
def profile(request, username):  

views.profile需要一個額外的參數與您沒有通過請求一起。

url(r'^$', views.profile, name='profile') 
+0

對不起,我不是最好的。我在哪裏傳遞這個論點? –

0

profile視圖需要一個username說法,但你的URL結構沒有定義它。行

url(r'^$', views.profile, name='profile')) 

更改爲類似

url(r'^profile/(?P<username>[\w\d]+)/$', views.profile, name='profile')) 

開頭的profile部分添加到您的index URL區別開來。

+0

嘿,謝謝你的幫助。我現在收到一個未找到頁面的錯誤消息,並且我確定它與將我的用戶名追加到網址末尾有關。 –

+0

您正在測試的URL是什麼?你如何傳遞用戶名? – Selcuk

+0

Url testing with:url(r'^(?P [a-zA-Z0-9] +)/ $',views.index,name ='index') –