Django class based view: passing additional information to the next view - python

I'm very new to Django and a bit overwhelmed by the documentation. I think my problem is pretty simple but everything i've found just confused me more.
I am building a little news app with a model NewsItem:
from django.db import models
from django.utils import timezone
# Create your models here.
class NewsItem(models.Model):
title = models.CharField(max_length=50)
newsText = models.TextField()
dateEntered = models.DateTimeField('date entered')
datePublished = models.DateTimeField('date published', blank=True, null=True)
user = models.CharField(max_length=30) #temporary field. will be changed to user foreign key
def __str__(self):
return self.title
def publish(self):
if (self.datePublished == None):
self.datePublished = timezone.now()
def published(self):
return self.datePublished != None
two views (technically 3) index and detail
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.views import generic
from .models import NewsItem
# Create your views here.
class IndexView(generic.ListView):
template_name = 'news/index.html'
context_object_name = 'latestNewsList'
def get_queryset(self):
return NewsItem.objects.order_by('-datePublished')[:5]
#todo
class DetailView(generic.DetailView):
model = NewsItem
template_name = 'news/detail.html'
def publish(request, itemId):
newsItem = get_object_or_404(NewsItem, pk=itemId)
newsItem.publish()
newsItem.save()
return HttpResponseRedirect(reverse('news:detail', args=(newsItem.id,)))
and an urlconf like this
from django.urls import path
from . import views
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:itemId>/publish', views.publish, name='publish'),
]
In the detail view i have a link Publish which just triggers the function views.publish. This view is supposed to redirect back to the detail view.
What i'm trying to do now is to display a little message (like article successfully published) in detail view when it was redirected by the publish view. But i have no idea what would be a good aproach
I could just render the details template in the publish view, but then it would still say news/publish in the URL instead of news/detail
Thanks in advance for your help

Have a look at the messages framework. You could add a success message before redirecting, which will be displayed on the next page.
from django.shortcuts import redirect
from django.contrib import messages
def publish(request, itemId):
newsItem = get_object_or_404(NewsItem, pk=itemId)
newsItem.publish()
newsItem.save()
messages.success(request, "The post has been published")
return redirect('news:detail', newsItem.id)
Note that I've simplified the return statement to use redirect(...) instead of HttpResponseRedirect(reverse(...)).

Related

Django : HttpRequest.__init__() takes 1 positional argument but 2 were given

So I simply want when I click on the particular article to output its slug, for example if the slug of the given article is django-rules then i want it to be outputted as django-rules when i click on it. just that
here is my model
from django.db import models
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField()
body = models.TextField()
date = models.DateTimeField(auto_now_add = True)
#add in tubnail and author later
def __str__(self):
return self.title
def snippet(self):
return self.body[:50]+'...'
Here is my views.py
from datetime import date
from django.shortcuts import render
from .models import Article
from django.http import HttpRequest
# Create your views here.
def article_list(request):
articles = Article.objects.all().order_by('date')
return render(request,'articles/article_list.html', {'articles': articles} )
def article_detail(request, slug):
return HttpRequest(slug)
url.py
from posixpath import relpath
from django.contrib import admin
from django.urls import path
from django.views.generic import TemplateView
from . import views
app_name = 'articles'
urlpatterns = [
path('', views.article_list, name = 'list'),
path('<slug:slug>/', views.article_detail, name = 'detail'),
]
Please dont suggest to add as_view()
its not working.
Instead of return HttpRequest(slug), you need to return HttpResponse(slug).

Django associate form values to user

I'm very new to Django but I started to create a project and I need some help. My question is very basic but I can't find the right answer.
I made a questionnaire that works fine but I don't know how to do that if a logged in user adds his answers Django associate the answers to the user.
Views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.template import loader
from .models import Stressz_teszt
from .forms import StresszForm
from django.forms import ModelForm
def stressz_item(request):
form = StresszForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('stressz/login.html')
return render(request, 'stressz/stressz-form.html', {'form':form})
Urls.py
from . import views
from django.urls import path
from django.forms import ModelForm
urlpatterns = [
path('', views.index, name='index'),
path('login/', views.login, name='login'),
#stressz teszt form
path('add', views.stressz_item, name='stressz_item'),
path('<int:item_id>/', views.detail, name='detail'),
Forms.py
from django import forms
from django.forms import ModelForm
from .models import Stressz_teszt
class StresszForm(forms.ModelForm):
class Meta:
model = Stressz_teszt
fields = ['stressz_v01', 'stressz_v02', 'stressz_v03', 'stressz_v04', 'stressz_v05',] ...
Models.py
from django.db import models
from django.contrib.auth.models import User
class Stressz_teszt(models.Model):
def __str__(self):
return self.stressz_name
user_name = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
stressz_name = models.CharField(max_length=200)
stressz_company = models.CharField(max_length=300)
stressz_v01 = models.IntegerField()
stressz_v02 = models.IntegerField()
stressz_v03 = models.IntegerField()
stressz_v04 = models.IntegerField()
stressz_v05 = models.IntegerField()
...
Thank you in advance!
You can do it like this :
def stressz_item(request):
if form.is_valid():
stressz_teszt = form.save(commit=False)
stressz_teszt.user_name = request.user
stressz_teszt.save()
return redirect('stressz/login.html')
return render(request, 'stressz/stressz-form.html', {'form': form})

Page not found (404) Request Method: GET ....... the current path, blog/blogpost, didn't match any of these

Hi i am trying to make blog website but while i fetch model with blogpost function in views.py its shows error that 404 page not found like ||Using the URLconf defined in mac.urls, Django tried these URL patterns, in this order: admin/ shop/ blog/ [name='BlogHome'] blog The current path, blog/blogpost, didn't match any of these. ||
-until i don't create model it works fine but after creating model and trying to fetch articles through post_id it throws error as page not found!
-what am i missing?
-Here is the codes i am using.
code of blog/urls.py ->
from django.urls import path
from . import views
urlpatterns = [
path(" ", views.index, name="ShopHome"),
path("blogpost/<int:id>/", views.blogpost, name="blogpost")]
code of blog/Views.py ->
from django.shortcuts import render
from .models import Blog
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'blog/index.html')
def blogpost(request,id):
post= Blog.objects.filter (post_id=id)[0]
print(post)
return render(request, 'blog/blogpost.html',{'post':post})
code of blog/adminpy ->
from django.contrib import admin
from .models import Blog
admin.site.register(Blog)
code of blog/models.py ->
from django.db import models
# Create your models here.
class Blog(models.Model):
post_id = models.AutoField(primary_key= True)
title = models.CharField(max_length=50)
title0 = models.CharField(max_length=500,default='')
title1= models.CharField(max_length=500,default='')
title2= models.CharField(max_length=500,default='')
Content_title= models.CharField(max_length=500,default='')
Content_title0= models.CharField(max_length=500,default='')
Content_title1= models.CharField(max_length=500,default='')
Content_title2= models.CharField(max_length=500,default='')
pub_date = models.DateField()
image = models.ImageField(upload_to='shop/images', default="")
def __str__(self):
return self.title
You are passing an /<int:id>/ (btw always include the last trailing slash in your urls /) but your path says /blog/blogpost with a str parameter where it should be an int representing the id of an existing Blogpost instance.

How do I make a function in django to add comments to my reviews

I have a website made on Django and the users able to leave a review and Ive tried to make it so they can add comments, however they add a comment it just saves it to the database rather then adding it to the post. If anyone knows how I could change my code so that it instead adds the comments to the review that they message on rather then them not appearing on the website at all.
views.py:
from django.shortcuts import render, redirect #Render is used to show the HTML, and redirect is used when the user needs to be sent to a different page automatically.
from .models import Review #This imports the Review database.
from .forms import ReviewForm, CommentForm #This imports the comments and review form.
from django.utils import timezone
from django.contrib.auth.decorators import login_required #This requires the user to be logged in.
def review(request):
return render(request, 'review/review.html')
#login_required(login_url='/accounts/login/')
def review_new(request):
if request.method == "POST":
form = ReviewForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.date = timezone.now()
post.author = request.user
post.save()
return redirect('/review')
else:
form=ReviewForm()
return render(request, 'review/post_edit.html', {'form': form})
def add_comment_to_post(request):
if request.method == "POST":
form = CommentForm(request.POST)
comment = form.save(commit=False)
comment.Review = review
comment.save() #This will save the comment to the database.
return redirect('reivew': review)
else:
form = CommentForm()
return render(request, 'review/add_comment_to_post.html', {'form': form})
models.py:
from django.db import models
from django.conf import settings
from django.utils import timezone
class Review(models.Model):
title = models.CharField(max_length=140)
body = models.TextField(max_length=3000)
date = models.DateTimeField()
author=models.ForeignKey(settings.AUTH_USER_MODEL, `on_delete=models.CASCADE, null=True)`
class comment(models.Model):
review = models.ForeignKey('review.Review', on_delete=models.CASCADE, related_name='comments', null=True)
author=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
text = models.TextField(max_length=3000)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.text
urls.py:
from django.urls import path, include, re_path
from django.views.generic import ListView, DetailView
from review.models import Review
from . import views
urlpatterns = [
path('', ListView.as_view(
queryset=Review.objects.all().order_by("-date")[:25],
template_name="review/review.html")),
re_path('(?P<pk>(\d+))',DetailView.as_view(model=Review, template_name="review/post.html")),
path('new/', views.review_new, name='review_new'),
path('new/comment', views.add_comment_to_post, name='add_comment_to_post'),
]
I can see that you should change comment.Review = review to something like comment.review = Review.objects.get(id=review_id) where review_id is an id passed from the form. Also notice that your Comment model allows the review to be null, which means currently you are adding comments without linking them correctly to the original post/review they should be related to.

Django TypeError get_object_or_404() takes at least 1 argument (0 given)

I'm making a blog and learning django as I go. I'm trying to incorporate urls that include both sluglines and post ids.
When I click on the "view on site" link in the admin panel, I get this error:
NoReverseMatch at /admin/r/7/2/ Reverse for 'article' with arguments '(u'test-post-3', '2')' and keyword arguments '{}' not found. 0 pattern(s) tried: []
When I manually enter the url, I get this error:
TypeError at /articles/test-post-3,2/ get_object_or_404() takes at least 1 argument (0 given)
Here's my code:
views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.core.urlresolvers import reverse
from django.views import generic
from django.utils import timezone
# Create your views here.
from articles.models import Content
class IndexView(generic.ListView):
template_name = 'articles/index.html'
context_object_name = 'latest_articles_list'
def get_queryset(self):
return Content.objects.filter(
published_date__lte=timezone.now()
).order_by('-published_date')[:5]
def detail(request, slugline, id):
article = get_object_or_404(pk=id)
return render(request, 'articles/detail.html', {'article': article})
urls.py:
from django.conf.urls import patterns, url
from articles import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name = 'index'),
#url(r'^(?P<slugline>[-\w\d]+), (?P<pk>\d+)/$', views.DetailView.as_view(), name='detail')
url(r'^(?P<slugline>[-\w\d]+),(?P<id>\d+)/$', view=views.detail, name='article'),
)
models.py:
from django.db import models
from django.db.models import permalink
from django.utils import timezone
import datetime
# Create your models here.
class Content(models.Model):
title = models.CharField(max_length=100, unique=True)
slugline = models.SlugField(max_length=100, unique=True)
body = models.TextField()
published_date = models.DateTimeField('date published')
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
# return ('article', (), {
# 'slugline': self.slugline,
# 'id': self.id,
# })
from django.core.urlresolvers import reverse
return reverse('article', args=[self.slugline, str(self.id)])
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.published_date <= now
was_published_recently.admin_order_field = 'published_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
main urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'the_Bluntist.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^articles/', include('articles.urls', namespace="articles")),
url(r'^admin/', include(admin.site.urls)),
)
You have a bad usage of get_object_or_404:
Docstring:
get_object_or_404(klass, *args, **kwargs)
Uses get() to return an object, or raises a Http404 exception if the object
does not exist.
klass may be a Model, Manager, or QuerySet object. All other passed
arguments and keyword arguments are used in the get() query.
Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
object is found.
You can make as following:
article = get_object_or_404(Article, pk=id)
article = get_object_or_404(Article.objects, pk=id))
article = get_object_or_404(Article.objects.all(), pk=id))
article = get_object_or_404(Article.objects.filter(pk=id))
article = get_object_or_404(pk=id)
You must pass the model and the filter to the call. Documentation here.
article = get_object_or_404(klass, pk=id)

Categories

Resources