Tôi đang cố gắng chuyển đổi tiêu đề bài đăng thành CamelCase để tạo một hashtag twitter, tôi đang sử dụng dải nhưng trả về một đối tượng thay vì tôi không biết đây có phải là đúng không?Chuyển đổi tiêu đề bài thành CamelCase
# views.py
def post_create(request):
if not request.user.is_authenticated():
raise Http404
form_class = PostCreateForm
if request.method == 'POST':
form = form_class(request.POST, request.FILES)
if form.is_valid():
instance = form.save(commit=False)
instance.creator = request.user
instance.slug = slugify(instance.title)
instance.hashtag = instance.title.strip()
instance.save()
slug = slugify(instance.title)
return redirect(instance.get_absolute_url())
else:
form = form_class()
context = {
'form': form,
}
return render(request, "posts/post_create.html", context)
nào trả <built-in method strip of unicode object at 0x031ECB48>
trong mẫu var, kết quả tôi đang tìm kiếm là như thế này MyPostTitle
vào mẫu
# Template view
<h3>#{{instance.hashtag|title}}</h3>
models.py
class Post(models.Model):
creator = models.ForeignKey(ProfileUser)
title = models.CharField(max_length=80)
hashtag = models.CharField(max_length=80)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.title
def get_absolute_url(self):
return reverse("posts:detail", kwargs={"slug": self.slug})
Bạn có thể hiển thị định nghĩa Mô hình bài đăng của mình không? Cảm ơn. – alecxe
Tôi đã thêm Mô hình bài đăng – linski
Được rồi, có thể là do tôi thiếu thứ gì đó hoặc không có đủ cà phê. Hãy thử loại bỏ các tập tin pyc và khởi động lại máy chủ - nó thực sự trông giống như nó đang chạy như 'instance.title.strip' thay vì' instance.title.strip() '. – alecxe