提交与此视图关联的表单时出现此错误.不确定究竟是什么问题,考虑到我有一个结构非常相似的表格,它工作正常.
#views.py
class Facture_Creer(SuccessMessageMixin, CreateView):
model = Facture
template_name = "facturation/nouvelle_facture.html"
form_class= FormulaireFacture
# permet de retourner a l'URL pointant vers le membre modifie
def get_success_url(self):
return reverse_lazy('facture_consulter',kwargs={'pk': self.get_object().id})
class Facture_Update(SuccessMessageMixin, UpdateView):
model = Facture
template_name = "facturation/nouvelle_facture.html"
form_class= FormulaireFacture
success_message = "Facture mise à jour avec succes"
# permet de retourner a l'URL pointant vers le membre modifie
def get_success_url(self):
return reverse_lazy('facture_consulter',kwargs={'pk': self.get_object().id})
#urls.py
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="facturation/index.html")),
url(r'^facture/$', FactureView.as_view()),
url(r'^facture/(?P<id>\d+)', FactureView.as_view(), name='facture_consulter'),
url(r'^facture/ajouter/$', Facture_Creer.as_view(), name='facture_creer'),
url(r'^facture/modifier/(?P<pk>\d+)/$', Facture_Update.as_view(), name='facture_update'),
url(r'^membre/ajouter/$', Membre_Creer.as_view(), name='membre_creer'),
url(r'^membre/modifier/(?P<pk>\d+)/$', Membre_Update.as_view(), name='membre_update'),
#url(r'membre/(?P<pk>\d+)/supprimer/$', Membre_Supp.as_view(), name='membre_delete')
)
urlpatterns += staticfiles_urlpatterns()
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 37
您需要传递一个对象标识符(pk或slug),以便您的视图知道它们正在操作哪个对象.
只是举一个你的例子urls.py:
url(r'^facture/ajouter/$', Facture_Creer.as_view(), name='facture_creer'),
url(r'^facture/modifier/(?P<pk>\d+)/$', Facture_Update.as_view(), name='facture_update'),
Run Code Online (Sandbox Code Playgroud)
看看第二个怎么样(?P<pk>\d+)/?这是将pk传递给UpdateView,因此它知道要使用哪个对象.因此,如果你去facture/modifier/5/,则UpdateView将修改pk为5的对象.
如果您不想在网址中传递pk或slug,则需要覆盖该get_object方法并以另一种方式获取对象.网址在这里.
Rob*_*bin 10
正如Alex建议的那样:对于默认的Django行为,你必须在你的url模式中使用"pk".
如果要将主键"pk"的对象标识符更改为其他名称,可以定义pk_url_kwarg.这是自Django 1.4以来的.
嘿,我使用了新path()功能,这是我的工作示例,我相信它会有所帮助:
视图.py:
from django.views.generic.detail import DetailView
class ContentAmpView(DetailView):
model = Content
template_name = 'content_amp.html' # Defaults to content_detail.html
Run Code Online (Sandbox Code Playgroud)
网址.py:
from django.urls import path
from .views import ContentAmpView
# My pk is a string so using a slug converter here intead of int
urlpatterns = [
path('<slug:pk>/amp', ContentAmpView.as_view(), name='content-amp'),
]
Run Code Online (Sandbox Code Playgroud)
模板/content_amp.html
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "Open-source framework for publishing content",
"datePublished": "2015-10-07T12:02:41Z",
"image": [
"logo.jpg"
]
}
</script>
<style amp-boilerplate>
body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}
</style>
<noscript>
<style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}
</style>
</noscript>
</head>
<body>
<h1>Welcome to AMP - {{ object.pk }}</h1>
<p>{{ object.titles.main }}</p>
<p>Reporter: {{ object.reporter }}</p>
<p>Date: {{ object.created_at|date }}</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
还要注意,在我的settings.py,下TEMPLATES,我有'APP_DIRS': True。更多关于这里的路径。
就像罗宾说的,如果你想要自定义PK名称,你可以使用pk_url_kwarg。
但除此之外,必须使用对象 pk 或 slug中的 slug 调用问题通用详细信息视图。
因此,如果您想创建自定义 slug 字段(无需使用pk或slug名称)。您可以在 DetailView 类中重写slug_field和。slug_url_kwarg
如果您希望 url 作为 Slug 字段,这只是另一个示例。
模型.py
class Post(models.Model):
title = models.CharField(max_length=255)
url = models.SlugField()
body = models.TextField()
image = models.ImageField(upload_to='blog/', blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
视图.py
class ListPost(ListView):
model = Post
template_name = 'blog/list.html'
paginate_by = 2
context_object_name = 'posts'
class DetailPost(DetailView):
model = Post
template_name = 'blog/detail.html'
slug_field = 'url'
slug_url_kwarg = 'url'
Run Code Online (Sandbox Code Playgroud)
urls.py
urlpatterns = [
path('', ListPost.as_view()),
path('<slug:url>/', DetailPost.as_view()),
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43332 次 |
| 最近记录: |