0
양식이 잘 렌더링되고 데이터가 잘 저장되며 관리 에서처럼 슬러그를 생성하지 않습니다. 나는 폼이 제출 될 때 슬러그가 자동으로 생성되기를 바라고있다. 당신의 도움을 주셔서 감사합니다.Django에서 양식 제출에 미리 채워진 필드를 추가하는 방법은 무엇입니까? (예 : admin에서)
코드 :
Models.py:
class Post(TimeStampActivate):
title = models.CharField(max_length=255,
help_text="Title of the post. Can be anything up to 255 characters.")
slug = models.SlugField()
excerpt = models.TextField(blank=True,
help_text="A small teaser of your content")
body = models.TextField()
publish_at= models.DateTimeField(default=datetime.datetime.now(),
help_text="Date and time post should become visible.")
blog = models.ForeignKey(Blog, related_name="posts")
tags = TaggableManager()
objects = PostManager()
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('post',(), {
'blog': self.blog.slug,
'slug': self.slug
})
class Meta:
ordering = ['-publish_at','-modified', '-created']
Views.py:
def add2(request):
if request.method == "POST":
form = BlogPostForm(request.POST)
if(form.is_valid()):
message = "thank you for your feedback"
form.save()
return render_to_response('add.html',
{'success':message},
context_instance=RequestContext(request))
else:
return render_to_response('add.html',
{'form':BlogPostForm},
context_instance=RequestContext(request))
Forms.py:
class BlogPostForm(forms.ModelForm):
class Meta:
model = Post
exclude = ('id', 'user', 'slug')
Admin.py:
class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
list_display =('active', 'title', 'excerpt', 'publish_at')
list_display_links=('title',)
list_editable = ('active',)
list_filter = ('modified', 'publish_at', 'active')
date_hierarchy = 'publish_at'
search_fields = ['title', 'excerpt', 'body', 'blog__name', 'blog__user__username']
fieldsets = (
(None, {
'fields': ('title', 'blog'),
}),
('Publication', {
'fields': ('active', 'publish_at'),
'description': "Control <strong>whether</strong> or not and when a post is visual to the world",
}),
('Content', {
'fields': ('excerpt', 'body', 'tags',),
}),
('Optional', {
'fields': ('slug',),
'classes': ('collapse',)
})
)
admin.site.register(Post, PostAdmin)
그래서 당신은 다음과 같은 뭔가를 자바 스크립트를 사용하는 것이 좋습니다합니까? – jmitchel3
달성하고자하는 목표에 따라 다릅니다. blogpost에게 슬러그를주기 위해서 서버 사이드 버전으로 갈 것입니다. – Jingo
징고, 도와 줘서 고마워. 난 아직도 stackoverflow 알고 그래서 내가 제대로 게시물에 응답하는 방법을 알아 냈어. 도와 주셔서 감사합니다. – jmitchel3