0

I have a project with a few apps, one of them is called blog. I'm trying to render a page for each single blogpost.

models.py (MyProject/Blog/Models)

class Blog(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    author = models.ForeignKey(User, max_length=40, blank=False)
    title = models.CharField(max_length=40, blank=False)

views.py (MyProject/Blog/Views)

def get_blog(request, blog_id):
    print "THIS STRING IS A TEST"
    blogpost = Blog.objects.get(pk=blog_id)
    context = {'blogpost': blogpost}
    return render(request, 'blog_page.html', context)

urls.py (MyProject/Urls)

urlpatterns = [
    url(r'^$', 'app_1.views.home', name='home'),    
    url(r'^blog/(?P<blog_id>\d+)/$','blog.views.get_blog'),

]

One of my blogposts has the following uuid: 98bfad8b-c44c-41d0-98d0-158379c3e362
Yet when I go to "example.com/BLOG/98bfad8b-c44c-41d0-98d0-158379c3e362", nothing happens.
It also doesn't print out 'THIS STRING IS A TEST', which tells me the view doesn't even get called.
It's my first time dealing with non-static URLS. What am I missing here?

Thanks in advance.

2 Answers 2

1

Your url regex is wrong \d+ any digit ( 0 through 9 ) so you have to make sure your regex capture words and -.

url(r'^blog/(?P<blog_id>[^/]+)$', 'blog.views.get_blog'),

You have to make sure blog is pointing to that particular view..

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Raja, this brings me one step closer, however this doesn't call my viewfunction.
It will definitely call your view function ... pls see the updated url
The view doesn't even get called, no teststring output, nore does it render a new page. Perhaps the uuid type i use has a different format? --> 'default=uuid.uuid4'..
1

write your url like this:

url(r'^blog/(?P<uuid>\w*)$', 'blog.views.get_blog'),

set blogpost variable like this:

blogpost = Blog.objects.get(uuid=uuid)

and pass the hex of your uuids to it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.