Модель Profile
class MyProfile(models.Model):
user = models.OneToOneField("auth.User")
date_of_birth = models.DateField(null=True)
bio = models.TextField()
avatar = models.ImageField(upload_to='uploads/', default='uploads\gallery\Berlin, Germany.jpg')
def __str__(self):
return self.user.username
Код представленения:
def blog_post_new(request):
if request.method == "POST":
form = BlogPostNewForms(request.POST or None)
if form.is_valid():
save_form = form.save(commit=False)
save_form.user = request.user
save_form.save()
joe = User.objects.get(username=request.user.username)
task = BlogPostNew.objects.get(slug=save_form.slug)
assign_perm('add_blogpostnew', joe, task)
assign_perm('change_blogpostnew', joe, task)
assign_perm('delete_blogpostnew', joe, task)
save_form.save()
return redirect('blog_post_detail', slug=save_form.slug)
else:
form = BlogPostNewForms()# По Traceback ошибка в этом месте
return render(request, 'pages/blog_post_edit.html', {'form': form})
Код формы полностью:
class BlogPostNewForms(ModelForm):
class Meta:
model = BlogPostNew
fields = '__all__'
exclude =['user','related_posts','featured_image', 'publish_date', 'short_description','slug','short_url','_meta_title','expiry_date','gen_description','in_sitemap','keywords','categories', 'description']
def __init__(self, *args, **kwargs):
super(BlogPostNewForms, self).__init__(*args, **kwargs)
if self.instance:
# self.fields['blog'].queryset = MyBlog.objects.filter(name='myblog')
self.fields['blog'].queryset = MyBlog.objects.filter(author=self.user) # И тут тоже ошибка