def dashnew(request): if request.method == 'POST': form = newPost(request.POST) if form.is_valid(): f = form.cleaned_data c = Cat.objects.get(pk=f['cat']) a = Post(title=f['title'],content=f['content'], date=datetime.datetime.now().date()) a.save() a.cat.add(c) a.save() return redirect('/dashboard/articles/' + str(a.pk)) else: return render(request, 'dash/dashErr.html') if request.method == 'GET': if request.user.is_authenticated: c = Cat.objects.all() snd = { "title": "New", "cat": c, } return render(request, 'dash/dashArticle.html', snd) else: return redirect('/admin')
def create_post(request): post_text = request.POST.get('the_post') response_data = {} post = Post(text=post_text, author=request.user) post.save() print request.POST.get('csrfmiddlewaretoken') response_data['result'] = 'Create post successful!' response_data['postpk'] = post.pk response_data['text'] = post.text response_data['created'] = post.created.strftime('%B %d, %Y %I:%M %p') response_data['author'] = post.author.username return HttpResponse(json.dumps(response_data), content_type="application/json")
def create_post(request): if request.method == 'POST': post_text = request.POST.get('the_post') response_data = {} post = Post(text=post_text, author=request.user) post.save() response_data['result'] = 'Create post successful!' response_data['postpk'] = post.pk response_data['text'] = post.text response_data['created'] = post.created.strftime('%B %d, %Y %I:%M %p') response_data['author'] = post.author.username return HttpResponse(json.dumps(response_data), content_type="application/json") else: return HttpResponse(json.dumps( {"nothing to see": "this isn't happening"}), content_type="application/json")
def create_post(request): if request.method == 'POST': post_text = request.POST.get('the_post') response_data = {} post = Post(text=post_text, author=request.user) post.save() response_data['result'] = 'Create POST successful' response_data['postpk'] = post.pk response_data['text'] = post.text response_data['created'] = post.created.strftime('%B %d, %Y %I:%M %p') response_data['author'] = post.author.username ''' In django 1.7, you can use the following to create a json http response from django.http import JsonResponse return JsonResponse(response_data) ''' return HttpResponse(json.dumps(response_data), content_type='application/json') else: return HttpResponse(json.dumps( {'nothing to see': 'this isnt happening'}), content_type='application/json')