您好我正在尝试实施一个分析系统,以便一旦用户登录,就会识别学生并从学生模型中提取信息并将其返回到主页,并可能返回系统中的每个其他页面。它还会将学生用户名附加到URL的末尾....希望
我收到此错误:
profile()缺少1个必需的位置参数:'用户名'
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 %}
答案 0 :(得分:1)
def profile(request, username):
views.profile会带有一个额外的参数以及您尚未传递的请求。
url(r'^$', views.profile, name='profile')
答案 1 :(得分:0)
您的profile
视图需要username
参数,但您的网址结构并未对其进行定义。改变行
url(r'^$', views.profile, name='profile'))
类似
url(r'^profile/(?P<username>[\w\d]+)/$', views.profile, name='profile'))
添加了开头的profile
部分,以区别于您的index
网址。