Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:36:59+00:00 2026-06-10T12:36:59+00:00

I’m learning Django REST Framework and working on this example; http://django-rest-framework.org/examples/blogpost.html but I’m getting

  • 0

I’m learning Django REST Framework and working on this example;

http://django-rest-framework.org/examples/blogpost.html

but I’m getting this error when I try to open http://localhost:8000/blog-post/

AttributeError at /blog-post/
'BlogPostResource' object has no attribute 'request'
Request Method: POST
Request URL:    http://localhost:8000/blog-post/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:    
'BlogPostResource' object has no attribute 'request'
Exception Location: /Users/wolfiem/PycharmProjects/TestRestApi/TestApp/resources.py in url, line 16
Python Executable:  /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.2
Python Path:    
['/Users/wolfiem/PycharmProjects/TestRestApi',
 '/Library/Python/2.7/site-packages/pexpect-2.4-py2.7.egg',
 '/Library/Python/2.7/site-packages/pythondialog-2.7-py2.7.egg',
 '/Library/Python/2.7/site-packages/pymongo-2.2.1-py2.7-macosx-10.7-intel.egg',
 '/Library/Python/2.7/site-packages/riak-1.4.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg',
 '/usr/local/lib/python2.7/site-packages',
 '/opt/local/bin',
 '/Library/Python/2.7/site-packages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

These are my files from the example:

resources.py

from djangorestframework.resources import ModelResource
from djangorestframework.resources import reverse
from TestApp.models import BlogPost,Comment

class BlogPostResource(ModelResource):
    """
    A Blog Post has a *title* and *content*, and can be associated with zero or more comments.
    """
    model = BlogPost
    fields = ('created','title','slug','content','url','comments')
    ordering = ('-created',)

    def url(self, instance):
        return reverse('blog-post',
                        kwargs = {'key':instance.key},
                        request=self.request)

    def comments(self, instance):
        return reverse('comments',
                        kwargs = {'blogpost':instance.key},
                        request=self.request)

class CommentResource(ModelResource):
    """
    A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*.
    """
    model = Comment
    fields = ('username','comment','created','rating','url','blogpost')
    ordering = ('-created',)

    def blogpost(self, instance):
        return reverse('blog-post',
                        kwargs={'key':instance.blogpost.key},
                        request=self.request)

models.py

from django.db import models
from django.template.defaultfilters import slugify
import uuid

def uuid_str():
    return str(uuid.uuid1())

# Create your models here.

RATING_CHOICES = ((0, 'Awful'),
    (1, 'Poor'),
    (2, 'OK'),
    (3, 'Good'),
    (4, 'Excellent'))

MAX_POSTS = 10

class BlogPost(models.Model):
    key = models.CharField(primary_key=True, max_length=64, default=uuid_str(), editable=False)
    title = models.CharField(max_length=128)
    content = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField(editable=False,default='')

    def encode(self):
        return self.encode('utf8')

    def save(self, force_insert=False, force_update=False, using=None):
        """
        Save
        """
        self.slug = slugify(self.title)
        super(self.__class__,self).save()
        for obj in self.__class__.objects.order_by('-created')[MAX_POSTS:]:
            obj.delete()

class Comment(models.Model):
    blogpost = models.ForeignKey(BlogPost,editable=False,related_name='comments')
    username = models.CharField(max_length=128)
    comment = models.TextField()
    rating = models.IntegerField(blank=True,null=True,choices=RATING_CHOICES,help_text='How did you rate this post?')
    created = models.DateTimeField(auto_now_add=True)

urls.py

from django.conf.urls import patterns, url
from djangorestframework.views import ListOrCreateModelView,InstanceModelView
from TestApp.resources import BlogPostResource,CommentResource

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'TestRestApi.views.home', name='home'),
    # url(r'^TestRestApi/', include('TestRestApi.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
#    url(r'^restframework', include('djangorestframework.urls', namespace='djangorestframework')),
    url(r'^$', ListOrCreateModelView.as_view(resource=BlogPostResource), name='blog-post-root'),
    url(r'^(?P<key>[^/]+)/$', InstanceModelView.as_view(resource=BlogPostResource), name='blog-post'),
    url(r'^(?P<blogpost>[^/]+)/comments/$',ListOrCreateModelView.as_view(resource=CommentResource), name='comments'),
    url(r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', InstanceModelView.as_view(resource=CommentResource)),
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:37:01+00:00Added an answer on June 10, 2026 at 12:37 pm

    I found the problem. I’ve edited the lines as below.

    def url(self, instance): 
    

    as

    def url(self,request,instance):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.