1
我怎樣才能添加ID段塞,這樣它會出現這樣的在Django中我怎麼能在塞標題添加ID
「這 - 是 - 一 - 交易-1」
其中「這是一筆交易」是標題,「1」是id號。在它被顯示爲"this-is-a-deal-None"
models.py ... 蛞蝓= models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
# Newly created object, so set slug
self.slug = '%s-%s' %(
slugify(self.deal_title),
str(self.id))
views.py
def deal_page(request, deal_title_slug):
# Create a context dictionary which we can pass to the template rendering engine.
context_dict = {}
try:
deal = SubmitDeal.objects.get(slug=deal_title_slug)
context_dict['deal_title'] = deal.deal_title
context_dict['deal'] = deal
except SubmitDeal.DoesNotExist:
# We get here if we didn't find the specified category.
# Don't do anything - the template displays the "no category" message for us.
pass
return render(request, 'deal_page.html', context_dict)
網址當前代碼的時刻。 py
url(r'^(?P<deal_title_slug>[\w\-]+)/$', 'submit_deals.views.deal_page', name='deal_page'),
admin.py
class SubmitModelAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug':('deal_title',)} #prepopulate slug with deal_title
class meta:
model = SubmitDeal
admin.site.register(SubmitDeal, SubmitModelAdmin)