urls.pyapp_name = 'Loader'urlpatterns = [ path('post_detail/', views.Loader_post_view.as_view(), name="post_detail"), path('post/', views.post.as_view(), name="post"), path('my_job/', views.Loader_post_list.as_view(), name="my_job"), path('delete/<int:pk>', views.Loader_post_delete.as_view(), name="Delete"), path('update/<int:pk>', views.Loader_post_update.as_view(template_name="post_detail.html"), name="Update")] 这是我 models.pyclass Loader_post(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Loader") pick_up_station = models.CharField(max_length=150) destination_station = models.CharField(max_length=150) sender_name = models.CharField(max_length=150) phone_number = PhoneNumberField(null=False, blank=False, unique=True) receiver_name = models.CharField(max_length=150) sending_item = models.CharField(max_length=150) #image_of_load = models.ImageField(default='',upload_to='static/img') weight = models.CharField(max_length=150) metric_unit = models.CharField(max_length=30, default='') quantity = models.PositiveIntegerField() pick_up_time = models.DateField() drop_time = models.DateField() paid_by = models.CharField(max_length=150) created_at = models.DateTimeField(auto_now=True) published_date = models.DateField(blank=True, null=True)def __str__(self): return self.user.usernamedef get_absolute_url(self): return reverse("Loader:post", kwargs={'pk': self.pk})如果我的 views.pyclass Loader_post_view(CreateView,LoginRequiredMixin): form_class = forms.Loader_post_form model = Loader_post template_name = "post_detail.html"def form_valid(self, form): print(form.cleaned_data) self.object = form.save(commit=False) self.object.user = self.request.user self.object.save() return super().form_valid(form)现在我的问题是,当我点击提交它显示错误和有时当我把'post'通过替换self,它将工作正常'pk':self.pk但是,如果新用户登录并尝试发布它不会存储到数据库中的数据,请帮助我找出这个错误
1 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
您的网址没有网址参数。您需要添加一个:postpk
app_name = 'Loader'
urlpatterns = [
path('post_detail/', views.Loader_post_view.as_view(), name="post_detail"),
path('post/<int:pk>', views.post.as_view(), name="post"),
path('my_job/', views.Loader_post_list.as_view(), name="my_job"),
path('delete/<int:pk>', views.Loader_post_delete.as_view(), name="Delete"),
path('update/<int:pk>', views.Loader_post_update.as_view(template_name="post_detail.html"),
name="Update")
]
添加回答
举报
0/150
提交
取消