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_note_to_client(self, author=None): note = Note( note=self, author=author, client=self.client, ) note.save()
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/')
def post(self, request): form = NoteForm(request.POST) if form.is_valid(): text = form.cleaned_data['post'] print("Add new note:", text) note = Note() note.text_message = text note.save() form = NoteForm() # Clear input field return render( request, self.tamplate_name, { 'notes': Note.objects.all().order_by("-unique_word_count"), 'form': form })
def post(self, request, *args, **kwargs): """ Adding a new note """ serializer = self.get_serializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: data = serializer.data note = Note(owner=request.user, title=data['title'], content=data['content']) note.save() data['id'] = note.pk return Response(data, status=status.HTTP_201_CREATED)
def add(request): if request.method=='GET': return render(request,'note/add_note.html') elif request.method=='POST': #处理数据 #考虑登录状态 uid=request.session.get('uid') if not uid: return HttpResponse('请先登录') user=User.objects.filter(id=uid) #todo 检查用户是否可以发表言论 user=user[0] title=request.POST.get('title') content=request.POST.get('content') #todo 检查 title content 是否提交 note=Note(title=title,content=content,user=user) note.save() return HttpResponseRedirect('/note/')
def register(request): form = UserCreationForm() if request.method == 'POST': form = UserCreationForm(request.POST) email = request.POST.get('email') username = request.POST.get('username') fullname = request.POST.get('fullname') if form.is_valid(): note = Note(title="Dummy note title", description="Dummy note description", username=username, active=True) note.save() form.save() messages.success(request, 'Your account has been created') return redirect('login') return render(request, 'register.html', {'form': form})
def add_note(request, **kwargs): user_id = request.user.id if request.is_ajax(): new_note = Note() new_note.user_id = user_id new_note.name = request.POST.get('name', '') if new_note.name == '': new_note.name = '제목 없음' new_note.content = request.POST.get('content', '') if new_note.content == '': new_note.content = '내용 없음' if request.POST.get('isNotPublic') == 'true': new_note.isPublic = False new_note.save() tmp_topic = request.POST.get('topic', '') tmp_topic = tmp_topic.split(':$&*:') tmp_topic.pop() for t in tmp_topic: chk_t = Topic.objects.filter(name__exact=t) # 동일한 Topic이 아직 없다면 if chk_t.count() == 0: topic = Topic() topic.name = t topic.save() new_note.topic.add(topic.id) # 동일한 Topic이 이미 있다면 else: new_note.topic.add(chk_t[0].id) return to_json({'status': 'success', 'id': new_note.id}) else: return to_json({'status': 'failed'})
def save_note(request): note_content = str.strip(request.POST['note_content']) note_id = request.POST['note_id'] if 3 < len(note_content) < 1000: try: if note_id: note = Note.objects.get(pk=note_id) else: note = Note() note.note = note_content note.save() return HttpResponseRedirect(reverse('home')) except: return render( request, 'add.html', { 'error_message': 'Internal server error', 'note_content': note_content }) else: return render( request, 'add.html', { 'error_message': 'Note length is invalid', 'note_content': note_content })