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

Time:09-22

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.

CodePudding user response:

Instead of return HttpRequest(slug), you need to return HttpResponse(slug).

  • Related