Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将pk从url传递到CreateView表单_有效_Python_Django_Django Forms_Django Views - Fatal编程技术网

Python 将pk从url传递到CreateView表单_有效

Python 将pk从url传递到CreateView表单_有效,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我想将其他模型的pk从URL传递到我的CreateView表单。怎么做?你能帮我吗?当我传递特定id时,它会工作,但我想从url自动获取pk。 我的视图.py: class ScenarioDetailView(LoginRequiredMixin, DetailView): model = Scenario class CommentCreateView(LoginRequiredMixin, CreateView): model = Comment fields =

我想将其他模型的pk从URL传递到我的CreateView表单。怎么做?你能帮我吗?当我传递特定id时,它会工作,但我想从url自动获取pk。 我的
视图.py

class ScenarioDetailView(LoginRequiredMixin, DetailView):
    model = Scenario

class CommentCreateView(LoginRequiredMixin, CreateView):
    model = Comment
    fields = [
        'commentText'
    ]

    def form_valid(self, form):
        form.instance.commentAuthor = self.request.user
        form.instance.commentDate = datetime.now()
        form.instance.commentScenario = Scenario.objects.get(pk=1) #there is my problem
        return super().form_valid(form)
path('scenario/<int:pk>/', ScenarioDetailView.as_view(), name='scenario-detail'),
我的
url.py

class ScenarioDetailView(LoginRequiredMixin, DetailView):
    model = Scenario

class CommentCreateView(LoginRequiredMixin, CreateView):
    model = Comment
    fields = [
        'commentText'
    ]

    def form_valid(self, form):
        form.instance.commentAuthor = self.request.user
        form.instance.commentDate = datetime.now()
        form.instance.commentScenario = Scenario.objects.get(pk=1) #there is my problem
        return super().form_valid(form)
path('scenario/<int:pk>/', ScenarioDetailView.as_view(), name='scenario-detail'),
有什么建议吗

编辑//
您可以从字典中获取值。例如:

# url
path('comment/<int:scenario_id>/', CommentCreateView.as_view(), name='comment-create'),

# view
class CommentCreateView(LoginRequiredMixin, CreateView):
    model = Comment
    fields = [
        'commentText'
    ]
    def form_valid(self, form):
            form.instance.commentAuthor = self.request.user
            form.instance.commentDate = datetime.now()
            form.instance.commentScenario = Scenario.objects.get(pk=self.kwargs.get('scenario_id')) #there is my problem
            return super().form_valid(form)
#url
路径('comment/',CommentCreateView.as_view(),name='comment-create'),
#看法
类CommentCreateView(LoginRequiredMixin,CreateView):
模型=注释
字段=[
“评论文本”
]
def表单_有效(自身、表单):
form.instance.commentAuthor=self.request.user
form.instance.commentDate=datetime.now()
form.instance.commentScenario=Scenario.objects.get(pk=self.kwargs.get('Scenario_id'))#这是我的问题
返回super().form_有效(form)

您可以从字典中获取值。例如:

# url
path('comment/<int:scenario_id>/', CommentCreateView.as_view(), name='comment-create'),

# view
class CommentCreateView(LoginRequiredMixin, CreateView):
    model = Comment
    fields = [
        'commentText'
    ]
    def form_valid(self, form):
            form.instance.commentAuthor = self.request.user
            form.instance.commentDate = datetime.now()
            form.instance.commentScenario = Scenario.objects.get(pk=self.kwargs.get('scenario_id')) #there is my problem
            return super().form_valid(form)
#url
路径('comment/',CommentCreateView.as_view(),name='comment-create'),
#看法
类CommentCreateView(LoginRequiredMixin,CreateView):
模型=注释
字段=[
“评论文本”
]
def表单_有效(自身、表单):
form.instance.commentAuthor=self.request.user
form.instance.commentDate=datetime.now()
form.instance.commentScenario=Scenario.objects.get(pk=self.kwargs.get('Scenario_id'))#这是我的问题
返回super().form_有效(form)

我更改了它,但现在我有一个错误:
为“comment create”反转,没有找到任何参数。
你在哪里开始反转?您可以共享完整的错误stacktrace吗?
异常类型:NoReverseMatch异常值:“comment create”的反向,未找到任何参数。尝试了1个模式:[“注释/(?P[0-9]+)/$”]
O,我找到了<代码>添加:{%csrf_token%}{{newcommon}}发布这是原因吗?我应该换吗?对不起,我的错。删除基于我上次评论的更改。请在模板中尝试此url:
{%url'comment create'scenario.pk%}
?我更改了它,但现在出现了一个错误:
为'comment create'反向,未找到任何参数。
您在哪里启动反向操作?您可以共享完整的错误stacktrace吗?
异常类型:NoReverseMatch异常值:“comment create”的反向,未找到任何参数。尝试了1个模式:[“注释/(?P[0-9]+)/$”]
O,我找到了<代码>添加:{%csrf_token%}{{newcommon}}发布这是原因吗?我应该换吗?对不起,我的错。删除基于我上次评论的更改。请在模板中尝试此url:
{%url'comment create'scenario.pk%}