Reputation: 13172
I have a urls.py file setup as follows
from django.conf.urls import patterns, include, url
from .views import *
urlpatterns = patterns('',
url(r'^$', BlogListView.as_view()),
url(r'(?P<blog_id>)\d{1,}/$', BlogDetailView.as_view())
)
with the correlating view
class BlogDetailView(View):
def get(self, request, blog_id, *args, **kwargs):
post = Blog.objects.get(post_id=blog_id).to_detail_json
return HttpResponse(json.dumps(post), mimetype='application/json')
I get an error when I visit 127.0.0.1:8000/blog/1/
ValueError at /blog/4/
invalid literal for int() with base 10: ''
but if I change
post = Blog.objects.get(post_id=blog_id).to_detail_json
to
post = Blog.objects.get(post_id=1).to_detail_json
then I get the correct response.
In case it is wanted, here is my model
from mongoengine import *
from collections import OrderedDict
import datetime
import json
class Blog(Document):
post_id = IntField(unique=True)
title = StringField(max_length=144, required=True)
date_created = DateTimeField(default=datetime.datetime.now)
body = StringField(required=True)
def __init__(self, *args, **kwargs):
self.schema = {
"title": self.title,
"date": str(self.date_created),
"id": self.post_id,
"body": self.body
}
super(Blog, self).__init__(*args, **kwargs)
@property
def to_detail_json(self):
fields = ["id","title", "date", "body"]
return {key:self.schema[key] for key in fields}
@property
def to_list_json(self):
fields = ["title", "date"]
return {key:self.schema[key] for key in fields}
I changed the BlogDetailView to return
return HttpResponse(json.dumps(self.kwargs),mimetype='application/json')
and it gives me
{
blog_id: ""
}
Which leads me to believe it is something with my urls.py file, but I do not see the error.
Upvotes: 4
Views: 1118
Reputation: 13172
It turns out that
url(r'(?P<blog_id>)\d{1,}/$', BlogDetailView.as_view())
should be
url(r'(?P<blog_id>\d{1,})/$', BlogDetailView.as_view())
Upvotes: 0
Reputation: 5048
try
post = Blog.objects.get(post_id=self.kwargs['blog_id']).to_detail_json
Upvotes: 3