Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

python - 在django中将pk传递给CreateView表单(pass pk to CreateView form in django)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to provide Intervention pk to your form.

(您需要在表单中提供Intervention pk。)

Simplest solution would be to just pass it with url:

(最简单的解决方案是将其传递给url:)

path('mark/create/<int:intervention_pk>', CreateEvaluationView.as_view(template_name="givemark.html"), name='newmark'),

It will require modification in your template to pass intervention pk, such as reverse('newmark', intervention_pk=intervention.pk)

(它将需要在模板中进行修改以传递干预pk,例如reverse('newmark', intervention_pk=intervention.pk))

Exclude intervention pk from your form, so your users will not be able to modify it accidentally.

(从您的表单中排除干预pk,以便您的用户将无法意外地对其进行修改。)

class NewEvaluation(forms.ModelForm):
    class Meta:
        model = Evaluation
        exclude = ('interventions',)

Set intervention pk right before you save your form:

(在保存表单之前设置干预pk:)

class CreateEvaluationView(CreateView):
    form_class = NewEvaluation
    template_name = 'mark.html'

    def form_valid(self, form):
        intervention = Intervention.objects.get(pk=self.kwargs['intervention_pk'])
        self.object = form.save(commit=False)
        self.object.interventions = intervention
        self.object.save()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...