def index(request): user=request.user if user.is_authenticated==False: user=User.objects.get(username="******") context={ 'username': user.username, 'userid': user.id, 'notes':user.notes.all() } #if post,save that note then display all note as normal if request.method=="POST": title=request.POST['titleinput'] content=request.POST['noteinput'] if len(title)==0 or len(content)==0: return render(request,"error.html",context={"errormsg": "empty title or note,please check your input"}) new_note=Note() new_note.user=user; new_note.level=3; new_note.title=request.POST['titleinput'] new_note.content=request.POST['noteinput'] new_note.save(); context['notes']=reversed(context['notes']) return render(request,"note/index.html",context=context) if 'keyword' in request.GET: #filter keyword in two context['notes']=context['notes'].filter(content__contains=request.GET['keyword']) | context['notes'].filter(title__contains=request.GET['keyword']) context['notes']=reversed(context['notes']) return render(request,"note/index.html",context=context)
def addnote(request): setting = Setting.objects.get(pk=1) if request.method == 'POST': form = NoteForm(request.POST, request.FILES) if form.is_valid(): current_user = request.user data = Note() # model ile bağlantı kur data.user_id = current_user.id data.category = form.cleaned_data['category'] data.title = form.cleaned_data['title'] data.keywords = form.cleaned_data['keywords'] data.description = form.cleaned_data['description'] data.image = form.cleaned_data['image'] data.detail = form.cleaned_data['detail'] data.slug = form.cleaned_data['slug'] data.status = 'False' data.save() # veritabanına kaydet messages.success(request, 'Your Content Insterted Successfuly') return HttpResponseRedirect('/user/notes') else: messages.success(request, 'Note Form Error:' + str(form.errors)) return HttpResponseRedirect('/user/addnote') else: category = Category.objects.all() form = NoteForm() context = { 'category': category, 'form': form, 'setting': setting, } return render(request, 'user_addnote.html', context)
def add_view(request): if request.method == 'GET': return render(request, 'note/add_note.html') elif request.method == 'POST': # 处理提交数据 title = request.POST.get('title') content = request.POST.get('content') # 存数据 username = request.session.get('username') user = User.objects.get(username=username) note = Note(user=user) note.title = title note.content = content note.save() return HttpResponseRedirect('/note/')