def ArticleRequest(request, article_id): if not request.user.is_authenticated(): return HttpResponseRedirect('/login/') art = Article.objects.get(id=article_id) user = Users.objects.get(id=art.user_id.id) users = request.user.get_profile() posts=Post.objects.filter(art_id=article_id) form=PostForm() sport_pic=Sport.objects.get(id=art.sport_id.id) gps_filename=str(art.gps_file) gps_filename=gps_filename[9:] if art.art_alb is not None: try: album = Album.objects.get(id=art.art_alb.id) except Album.DoesNotExist: album=None pic_list = Picture.objects.filter(album=album) context =({ 'art':art, 'user1':user, 'pic_list':pic_list, 'users':users, 'posts':posts, 'form':form, 'sport_pic':sport_pic, 'gps_filename':gps_filename, }) else: context=({ 'art':art, 'user1':user, 'users':users, 'posts':posts, 'form':form, 'sport_pic':sport_pic, 'gps_filename':gps_filename, }) if request.method=='POST': form = PostForm(request.POST) if form.is_valid(): content=form.cleaned_data['content'] new_post=Post(user_id=users,art_id=art,content=content,date=datetime.datetime.now()) new_post.save() return render_to_response('main/article.html', context, context_instance=RequestContext(request))
def post_request(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): fm = form.save(commit=False) fm.owner = request.user fm.save() form.save() messages.success(request, f'Your request was posted!') return HttpResponseRedirect(reverse('home')) else: form = PostForm() return render(request, 'mainapp/post_page.html', {'form': form})
def post_edit(request, username, post_id): profile = get_object_or_404(User, username=username) post = get_object_or_404(Post, pk=post_id, author=profile) if request.user != profile: return redirect('index') form = PostForm(request.POST or None, files=request.FILES or None, instance=post) if request.method == 'POST': if form.is_valid(): form.save() return redirect("index") return render( request, 'func/post_edit.html', { 'form': form, 'post': post }, )
def server_error(request): return render(request, "mics/500.html", status=500) form = PostForm(instance=Post.objects.get(id=post_id)) if username != request.user.username: HttpResponse("ERROR") if request.method == "POST": form = PostForm(request.POST, instance=Post.objects.get(id=post_id)) if form.is_valid(): form.save() return redirect('index') return render(request, 'edit_post.html', {'form': form})
def new_post(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.save() return redirect('index') return render(request, 'func/new_post.html', {"form": form}) form = PostForm() return render(request, 'func/new_post.html', {'form': form})