def create_blog(request): user = request.user profile = Profile.objects.get(user=request.user) context = {'profile': profile} if request.method == 'GET': return render(request, 'blogs/create_blog.html', context=context) if request.method == 'POST': title = request.POST['title'] text = request.POST['text'] tags = request.POST['tags'] # blog create blog = Blog() blog.title = title blog.text = text blog.author = profile try: if any(request.FILES): blog.image = request.FILES['image'] except: blog.tags.add(tags) messages.error(request, 'Blog Image is not correct!') return render(request, 'blog/create_blog.html', context=context) blog.save() messages.success(request, 'Blog successfully created!') return redirect('/profiles/profile/{}'.format(user.username))
def create_blog(request): if request.method == "GET": context = {} return render(request, 'blogs/create_blog.html', context=context) if request.method == "POST": blog = Blog() blog.author = request.user.profile blog.title = request.POST['title'] blog.text = request.POST['text'] #тэги tags = request.POST.get('tags').split( ",") #делим строку из формы по запятым tags = [i.strip() for i in tags] #режем пробелы с обеих сторон #---------------------------------------------- try: if any(request.FILES): blog.image = request.FILES['image'] except: messages.error(reguest, 'Image is not correct!') return redirect(reverse('pages:index')) blog.likes = [] blog.save() blog.tags.set( *tags) #назначаем тэги в свежесохранённому блогу(или наоборот) messages.success(request, 'Blog successfuly created!') return redirect(reverse('pages:index'))