找不到关键字参数为“{'pk': 8}”的“post”的反面。已尝试 1 种模式:

urls.py


app_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.py


class 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.username


def get_absolute_url(self):

    return reverse("Loader:post", kwargs={'pk': self.pk})

如果我的 views.py


class 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但是,如果新用户登录并尝试发布它不会存储到数据库中的数据,请帮助我找出这个错误


开满天机
浏览 72回答 1
1回答

宝慕林4294392

您的网址没有网址参数。您需要添加一个:postpkapp_name = 'Loader'urlpatterns = [&nbsp; &nbsp; path('post_detail/', views.Loader_post_view.as_view(), name="post_detail"),&nbsp; &nbsp; path('post/<int:pk>', views.post.as_view(), name="post"),&nbsp; &nbsp; path('my_job/', views.Loader_post_list.as_view(), name="my_job"),&nbsp; &nbsp; path('delete/<int:pk>', views.Loader_post_delete.as_view(), name="Delete"),&nbsp; &nbsp; path('update/<int:pk>', views.Loader_post_update.as_view(template_name="post_detail.html"),&nbsp;&nbsp; &nbsp; name="Update")]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python