def addnews(request): if request.method == 'POST': form = NewsForm(request.POST, request.FILES) if form.is_valid(): current_user = request.user data = News() data.user_id = current_user.id 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.category = form.cleaned_data['category'] data.slug = form.cleaned_data['slug'] data.detail = form.cleaned_data['detail'] data.status = 'New' data.save() messages.success(request, 'Your news has been added!') return HttpResponseRedirect('/user/mynews') else: messages.warning(request, "Error") return HttpResponseRedirect('/user/addnews') else: news = News.objects.all() category = Category.objects.all() menu = Menu.objects.all() form = NewsForm() context = { 'category': category, 'form': form, 'menu': menu, 'news': news, } return render(request, 'user_addnews.html', context)
def add(request): if request.method == 'POST': form = NewsForm(request.POST) news = form.save(commit=False) news.author = request.user news.save() request.user.message_set.create(message='L\'avis a bien été ajouté') return HttpResponseRedirect('/news/') else: form = NewsForm() return render_to_response('news/add_news.html', {'form': form},RequestContext(request))
def add(request): if request.method == 'POST': form = NewsForm(request.POST) news = form.save(commit=False) news.author = request.user news.save() request.user.message_set.create(message='L\'avis a bien été ajouté') return HttpResponseRedirect('/news/') else: form = NewsForm() return render_to_response('news/add_news.html', {'form': form}, RequestContext(request))
def edit(request, news_id): try: ref = request.META.get('HTTP_REFERER') n = News.objects.get(pk=news_id) if request.method == 'POST': form = NewsForm(request.POST, instance=n) if form.is_valid(): form.save() request.user.message_set.create(message='L\'avis a bien été modifié') else: request.user.message_set.create(message='L\'avis n a pas été modifié') return HttpResponseRedirect('/news/') else: form = NewsForm(instance=n) return render_to_response('news/edit_news.html', {'form': form,'ref': ref},RequestContext(request)) except News.DoesNotExist: request.user.message_set.create(message='L\'avis demandé n\'existe pas') return HttpResponseRedirect('/news/')
def newsedit(request, id): news = News.objects.get(id=id) if request.method == 'POST': form = NewsForm(request.POST, request.FILES, instance=news) if form.is_valid(): form.save() messages.success(request, 'Your news has been updated!') return HttpResponseRedirect('/user/mynews') else: messages.warning(request, "Error") return HttpResponseRedirect('/user/newsedit' + str(id)) else: category = Category.objects.all() menu = Menu.objects.all() form = NewsForm(instance=news) context = { 'category': category, 'form': form, 'menu': menu, 'news': news, } return render(request, 'user_addnews.html', context)
def edit(request, news_id): try: ref = request.META.get('HTTP_REFERER') n = News.objects.get(pk=news_id) if request.method == 'POST': form = NewsForm(request.POST, instance=n) if form.is_valid(): form.save() request.user.message_set.create( message='L\'avis a bien été modifié') else: request.user.message_set.create( message='L\'avis n a pas été modifié') return HttpResponseRedirect('/news/') else: form = NewsForm(instance=n) return render_to_response('news/edit_news.html', { 'form': form, 'ref': ref }, RequestContext(request)) except News.DoesNotExist: request.user.message_set.create( message='L\'avis demandé n\'existe pas') return HttpResponseRedirect('/news/')