def new_post(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = Post() post.title = request.POST.get('title') post.description = request.POST.get('description') post.detailed_description = request.POST.get( 'detailed_description') post.tags = request.POST.getlist('tags')[0].split(' ') post.author = request.user.get_full_name().split(" ")[0] post.publication_date = datetime.date.today() post.email = request.POST.get('email') post.save() return HttpResponseRedirect('/board/') else: form = PostForm() form.fields['author'].initial = request.user.username return render(request, 'form.html', {'form': form})
def setUpTestData(cls): # create user user = User.objects.create(name=name, email=email, password=password) # get token when login request = RequestFactory().post('/login/', data=json.dumps({ 'email': email, 'password': password }), content_type='application/json') global token token = json.loads(login(request=request).content)['token'] # create a post post = Post() post.author = user post.content = "測試用的發文" post.save() # get post_id for post_item in Post.objects.filter(content="測試用的發文"): global post_id post_id = post_item.id