Pre-populate 将字段插入到 Django 站点的表单字段中
Pre-populate slug field into a form field of a Django site
我创建了一个用于在网站上发布 post 的表单。在模型中有一个 SlugField,它是 admin.py 中的一个 pre-populated 字段,用于 post.
的标题
forms.py
class TestPostModelForm(forms.ModelForm):
title = forms.CharField(
max_length=70,
label="Titolo",
help_text="Write post title here. The title must be have max 70 characters",
widget=forms.TextInput(attrs={"class": "form-control form-control-lg"}),
)
slug_post = forms.SlugField(
max_length=70,
label="Slug",
help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents",
widget=forms.TextInput(attrs={"class": "form-control form-control-sm"}),
)
.....
class Meta:
model = TestPostModel
fields = [
"title",
"slug_post",
"description",
"contents",
....
]
如果我从管理面板创建一个 post,则 slug 会自动正确填充,但如果我从表单创建一个 post,则不会发生同样的事情。在第二种情况下,创建了 post 但 slug 字段仍然为空。
我读到我必须使用 slugify 在我的表单中创建一个 pre-populated 字段,但我不清楚我可以用哪种方法来做到这一点。
我可以举个例子吗?
这是示例,在您的 views.py
form = PostForm(request.POST):
if form.is_valid():
post = form.save(commit=False)
post.slug = slugify(post.title)
post.save()
...
关于 coderasha 指示的一些精确说明:从表单中删除 slug 字段很重要。
就我而言:
class TestPostModelForm(forms.ModelForm):
title = forms.CharField(
max_length=70,
label="Titolo",
help_text="Write post title here. The title must be have max 70 characters",
widget=forms.TextInput(attrs={"class": "form-control form-control-lg"}),
)
.....
class Meta:
model = TestPostModel
fields = [
"title",
"description",
"contents",
....
]
class Post(models.Model):
title = models.CharField(max_length=150)
image = models.ImageField(upload_to='images/', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(unique=True)
def pre_save_slugify_receiver(sender, instance, *args, **kwargs):
slug = slugify(instance.title)
instance.slug = slug
pre_save.connect(pre_save_slugify_receiver, sender=Post)
只是想加上我的两分钱,因为我自己也在努力解决这个问题,因为自动获得了 slug。以下是 forms.py 和 views.py 中的示例。 if "POST" 部分是 return 回到首页。否则出示表格。
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'intro','body', 'status']
其中 Post 的模型有许多其他字段,例如 'slug'、'published'、'created'、'updated' 等
views.py
def add_post(request):
if request.method == 'POST':
#a post was added
post_form = PostForm(data=request.POST)
if post_form.is_valid():
#create a post object but don't save to database yet
new_post = post_form.save(commit=False)
#assign the current slug and user to the post
new_post.author = request.user
new_post.slug = slugify(new_post.title)
#save post to database
new_post.save()
return HttpResponsePermanentRedirect(reverse('blog:post_list'))
else:
post_form = PostForm()
return render(request, 'blog/post/add_post.html', {'post_form': post_form})
要访问模板中的表单,您可以使用以下命令。
HTML
<form method="post">
{{ post_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Post" style="cursor: pointer;"></p>
</form>
我创建了一个用于在网站上发布 post 的表单。在模型中有一个 SlugField,它是 admin.py 中的一个 pre-populated 字段,用于 post.
的标题forms.py
class TestPostModelForm(forms.ModelForm):
title = forms.CharField(
max_length=70,
label="Titolo",
help_text="Write post title here. The title must be have max 70 characters",
widget=forms.TextInput(attrs={"class": "form-control form-control-lg"}),
)
slug_post = forms.SlugField(
max_length=70,
label="Slug",
help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents",
widget=forms.TextInput(attrs={"class": "form-control form-control-sm"}),
)
.....
class Meta:
model = TestPostModel
fields = [
"title",
"slug_post",
"description",
"contents",
....
]
如果我从管理面板创建一个 post,则 slug 会自动正确填充,但如果我从表单创建一个 post,则不会发生同样的事情。在第二种情况下,创建了 post 但 slug 字段仍然为空。
我读到我必须使用 slugify 在我的表单中创建一个 pre-populated 字段,但我不清楚我可以用哪种方法来做到这一点。
我可以举个例子吗?
这是示例,在您的 views.py
form = PostForm(request.POST):
if form.is_valid():
post = form.save(commit=False)
post.slug = slugify(post.title)
post.save()
...
关于 coderasha 指示的一些精确说明:从表单中删除 slug 字段很重要。
就我而言:
class TestPostModelForm(forms.ModelForm):
title = forms.CharField(
max_length=70,
label="Titolo",
help_text="Write post title here. The title must be have max 70 characters",
widget=forms.TextInput(attrs={"class": "form-control form-control-lg"}),
)
.....
class Meta:
model = TestPostModel
fields = [
"title",
"description",
"contents",
....
]
class Post(models.Model):
title = models.CharField(max_length=150)
image = models.ImageField(upload_to='images/', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(unique=True)
def pre_save_slugify_receiver(sender, instance, *args, **kwargs):
slug = slugify(instance.title)
instance.slug = slug
pre_save.connect(pre_save_slugify_receiver, sender=Post)
只是想加上我的两分钱,因为我自己也在努力解决这个问题,因为自动获得了 slug。以下是 forms.py 和 views.py 中的示例。 if "POST" 部分是 return 回到首页。否则出示表格。
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'intro','body', 'status']
其中 Post 的模型有许多其他字段,例如 'slug'、'published'、'created'、'updated' 等
views.py
def add_post(request):
if request.method == 'POST':
#a post was added
post_form = PostForm(data=request.POST)
if post_form.is_valid():
#create a post object but don't save to database yet
new_post = post_form.save(commit=False)
#assign the current slug and user to the post
new_post.author = request.user
new_post.slug = slugify(new_post.title)
#save post to database
new_post.save()
return HttpResponsePermanentRedirect(reverse('blog:post_list'))
else:
post_form = PostForm()
return render(request, 'blog/post/add_post.html', {'post_form': post_form})
要访问模板中的表单,您可以使用以下命令。
HTML
<form method="post">
{{ post_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Post" style="cursor: pointer;"></p>
</form>