在Django中的UpdateView和CreateView之间传递数据

时间:2018-05-31 10:04:31

标签: django python-3.x web-services dynamically-generated create-view

我有两个型号Permit&场。

class Permit(models.Model):
    name = models.CharField(max_length=50)
    skill_course = models.ManyToManyField(Skill, related_name="+", blank=True)

class Course(models.Model):
    name = models.CharField(max_length=50)
    skills = models.ManyToManyField(Skill, related_name="courses",blank=True,)
    students = models.ManyToManyField(User, related_name="courses",blank=True,)

单击许可对象将显示动态获取许可的学生(在使用GET方法的UpdateView中)。

如何与这些学生一起创建课程?(在使用POST方法的CreateView中)

我是否必须在2个视图之间传递数据?我想创建一个utils.py文件,创建一个方法,我将在其他方法或类中重用数据,但类或函数将有requestpk,我不知道如何返回一个字典,因为它应该有请求和模板对吗?我被困在这里,非常感谢任何帮助,提前谢谢......

1 个答案:

答案 0 :(得分:0)

首先,您应该在setting.py中激活session middleware

然后使用这样的东西:

   def update_project_filter(request):
    ...
    selected_project_id = project_form.cleaned_data["Project_Name"].id
    request.session['selected_project_id'] = selected_project_id
    ...

def update_project(request):
    ...
    selected_project_id = request.session.get('selected_project_id')
    ...def update_project_filter(request):
    ...
    selected_project_id = project_form.cleaned_data["Project_Name"].id
    request.session['selected_project_id'] = selected_project_id
    ...

def update_project(request):
    ...
    selected_project_id = request.session.get('selected_project_id')
    ...