i'm new to django.
(我是django的新手。)
I have a model that looks like this:
(我有一个看起来像这样的模型:)
models.py
(models.py)
class Intervention(models.Model):
subject = models.CharField(max_length=200)
begin_date = models.DateField(default=datetime.datetime.today)
end_date = models.DateField(default=datetime.datetime.today)
description = models.TextField(blank=True)
speaker = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
campus = models.ForeignKey(Campus, on_delete=models.CASCADE)
class Meta:
verbose_name = 'Intervention'
verbose_name_plural = 'Interventions'
def __str__(self):
return self.subject
class Evaluation(models.Model):
interventions = models.ForeignKey(Intervention, on_delete=models.CASCADE)
student_id = models.CharField(max_length=20)
speaker_knowledge_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
speaker_teaching_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
speaker_answer_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
slide_content_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
slide_examples_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
comment = models.TextField(blank=True)
The idea is when that the student enter the website on home page he have to choose the campus where he study then he is redirected to a page where he see only interventions of his campus then when he choose the intervention he get the detail of it :
(这个想法是,当学生进入主页上的网站时,他必须选择他要学习的校园,然后将他重定向到一个页面,在该页面上他仅看到校园的干预措施,然后当他选择干预措施时,他会获得详细信息:)
Home page screen
(主页画面)
Interventions page
(干预页面)
Intervetion detail page
(介入细节页面)
Everything is working so far.
(到目前为止,一切正常。)
Now on "intervention detail page" when the user click on "give mark" he is redirected to a page to create mark (i use CreateView class-based) like below: (现在,在“干预详细信息页面”上,当用户单击“给定标记”时,他将被重定向到页面以创建标记(我使用基于CreateView类的标记),如下所示:)
create mark
(创建标记)
Now my question is how can i replace Modelchoicefield in the generated form by pk of the intervetion that student want to give a mark?
(现在我的问题是我该如何用学生要打分的干预pk替换生成的表格中的Modelchoicefield?)
Views.py
(Views.py)
class CreateEvaluationView(CreateView):
form_class = NewEvaluation
template_name = 'mark.html'
def home(request):
campus = Campus.objects.all().order_by('-name')
return render(request, 'home.html', {'campus': campus})
def mark(request):
if request.method == 'POST':
campus = request.POST.get('campus')
intervention = Intervention.objects.filter(campus=campus)
return render(request, 'mark.html', {'intervention': intervention})
def intervention_detail(request, pk):
intervention_detail = get_object_or_404(Intervention, pk=pk)
return render(request, 'intervention_detail.html', {'intervention_detail': intervention_detail})
urls.py
(urls.py)
path('', views.home, name='home'),
path('mark/', views.mark, name='mark'),
path('mark/<int:pk>/', views.intervention_detail, name='intervention_detail'),
path('mark/create/', CreateEvaluationView.as_view(template_name="givemark.html"), name='newmark'),
path('intervention/create/', login_required(CreateInterventionView.as_view(template_name="intervention_create.html")), name='create'),
path('intervention/', login_required(ListInterventionView.as_view(template_name="intervention_list.html")), name='list'),
Thanks in advance for your help!
(在此先感谢您的帮助!)
Bast regards. (谨此致意。)
ask by came leon translate from so