我想为俱乐部活动注册和检查页面制作一个页面。所以在我检查页面中的学生之后,我想要显示所有学生的姓名和他们的'atclub' - 该学生将加入。
但显示列表存在一些问题。
我做了一个'俱乐部'课程和几个实例。然后我把学生作为俱乐部的外键。像这样。
在models.py
中class Club(models.Model):
club = models.CharField(max_length=30)
def __str__(self):
return self.club
#동아리
class Student(models.Model):
student = models.CharField(max_length=30)
clubs = models.ManyToManyField(Club, related_name='inclubs')
class_s = models.ForeignKey(Class, on_delete=models.CASCADE)
isfilled = models.BooleanField(default=False)
atclub = models.ForeignKey(Club, on_delete=models.CASCADE) #활동 동아리
def __str__(self):
return self.student
我想让哪位俱乐部学生参与其中。所以我写了,
在我的results_view.html
中<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>동아리 신청 확인</title>
</head>
<body>
<ul>
{% for student in a %}
<div style="text-align:center">
<li>{{student.student}}--{{student.atclub}}</li>
</div>
{% endfor %}
</ul>
<a href="{% url 'club:index'%}">메인으로 돌아가기</a>
</body>
</html>
这是我的views.py
def register(request, club_id):
club = get_object_or_404(Club, pk=club_id)
error_message = None
if request.method == 'POST':
students = request.POST.getlist('students[]')
if students:
for studentid in students:
studentss = get_object_or_404(Student, pk=studentid)
studentss.atclub = club
studentss.save()
return HttpResponseRedirect(reverse('club:results'))
else:
error_message = "학생을 선택하지 않았습니다."
context = {'club':club,'error_message':error_message,}
return render(request, 'club/detail.html', context)
def results(request):
a = Student.objects.all()
return render(request, 'club/results.html', {'a':a})
错误消息
ValueError at /club/results/
invalid literal for int() with base 10: 'CREATOR'
Request Method: GET
Request URL: http://127.0.0.1:8000/club/results/
Django Version: 2.0.5
Exception Type: ValueError
Exception Value:
invalid literal for int() with base 10: 'CREATOR'
Exception Location: C:\anaconda3\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 947
Python Executable: C:\anaconda3\python.exe
Python Version: 3.6.4
Python Path:
['C:\\Users\\김예지\\GSAv2',
'C:\\anaconda3\\python36.zip',
'C:\\anaconda3\\DLLs',
'C:\\anaconda3\\lib',
'C:\\anaconda3',
'C:\\anaconda3\\lib\\site-packages',
'C:\\anaconda3\\lib\\site-packages\\win32',
'C:\\anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\anaconda3\\lib\\site-packages\\Pythonwin']
Server time: Tue, 12 Jun 2018 12:11:11 +0000
Error during template rendering
In template C:\Users\김예지\GSAv2\club\templates\club\results.html, error at line 11
invalid literal for int() with base 10: 'CREATOR'
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>동아리 신청 확인</title>
6 </head>
7 <body>
8 <ul>
9 {% for student in a %}
10 <div style="text-align:center">
11 <li>{{student.student}}--{{student.atclub}}</li>
12 </div>
13 {% endfor %}
14 </ul>
15 <a href="{% url 'club:index'%}">메인으로 돌아가기</a>
16 </body>
17 </html>
18
19 <!--
20 값을 출력하는 부분이다.
21 -->
Traceback Switch to copy-and-paste view
C:\anaconda3\lib\site-packages\django\template\base.py in _resolve_lookup
current = current[bit] ...
▶ Local vars
During handling of the above exception ('Student' object is not subscriptable), another exception occurred:
C:\anaconda3\lib\site-packages\django\db\models\fields\related_descriptors.py in __get__
rel_obj = self.field.get_cached_value(instance) ...
▶ Local vars
C:\anaconda3\lib\site-packages\django\db\models\fields\mixins.py in get_cached_value
return instance._state.fields_cache[cache_name] ...
▶ Local vars
During handling of the above exception ('atclub'), another exception occurred:
C:\anaconda3\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request) ...
▶ Local vars
C:\anaconda3\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
C:\anaconda3\lib\site-packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\김예지\GSAv2\club\views.py in results
**return render(request, 'club/results.html', {'a':a}) ...**
▶ Local vars
a
<QuerySet [<Student: 1101 AAA>, <Student: 1102 BBB>, <Student: 1103 CCC>, <Student: 1104 DDD>, <Student: 1105 EEE>, <Student: 1201 aaa>, <Student: 1202 bbb>, <Student: 1203 ccc>, <Student: 1204 ddd>, <Student: 1205 eee>]>
request
<WSGIRequest: GET '/club/results/'>
我该如何解决这个错误?请帮帮我! :(:)