Django Blog App urls functionality - django

I am trying to integrate the following blog APP https://github.com/nathanborror/django-basic-apps ,so in my main urls.py i have included the blog urls as (r'^blog/',include('basic.blog.urls')), Now my question is that now when i point my browser to the blog APP http://127.0.0.1/blog/ i get a message as "Post archive",How to proceed from here i.e, how to post blog and retrieve the same.What is the url to be used..The following is the blog urls
from django.conf.urls.defaults import *
urlpatterns = patterns('basic.blog.views',
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
view='post_detail',
name='blog_detail'
),
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{1,2})/$',
view='post_archive_day',
name='blog_archive_day'
),
url(r'^(?P<year>\d{4})/(?P<month>\w{3})/$',
view='post_archive_month',
name='blog_archive_month'
),
url(r'^(?P<year>\d{4})/$',
view='post_archive_year',
name='blog_archive_year'
),
url(r'^categories/(?P<slug>[-\w]+)/$',
view='category_detail',
name='blog_category_detail'
),
url (r'^categories/$',
view='category_list',
name='blog_category_list'
),
url(r'^tags/(?P<slug>[-\w]+)/$',
view='tag_detail',
name='blog_tag_detail'
),
url (r'^search/$',
view='search',
name='blog_search'
),
url(r'^page/(?P<page>\d+)/$',
view='post_list',
name='blog_index_paginated'
),
url(r'^$',
view='post_list',
name='blog_index'
),
)

I have never used this blogging app, but im guessing because it suggests its "basic" it will just provide the bare bones. So my starting point would be to add a post and see what happens.
If going to /blog/ doesnt provide a way to add a post, then register the models with your admin site and add through that way. Im guessing that you may have to build your own adding sections...
If you dont want to do this, djang-blog-zinnia is a blogging app i have used, and really like it

There are no URLs specific to post creation, so you need to do it via the Admin interface. I looked at the code and there is a specific template for creating/modifying post objects.

I've used and customized the basic blog application
The creation can be done through the Admin interface, the models are already registered with the admin interface
You just need to provide a good layout and some cool stylesheets for your front end to get started

Related

Integrate Django Oauth Toolkit urls correctly

I followed the instructions from the official django-oauth toolkit doc (https://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial_02.html) and included all oauth-toolkit urls manually to prevent users from creating applications.
In my root urls.py i added the following line to the url patterns, as showed in the tutorial:
url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider")),
I used the include from django.conf.url package.
Now when i start the server, it says
"Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead."
when i add a name attribute to the url-command like this:
url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider"), name="oauth2_provider"),
the server starts up, but when i try to visit "localhost/o/applications" it says "NoReverseMatch: 'oauth2_provider' is not a registered namespace"
What am i doing wrong?
Try
url(r'^o/', include(('oauth2_provider.urls', 'oauth2_provider_app', ), namespace='oauth2_provider'), ),
Refer oauth2_provider

Django URL reversing NoReverseMatch issue

I'm trying to use reverse to redirect a user to a login page from a third party App I'm using.
The URL config has:
urlpatterns = [
# authentication / association
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
name='begin'),
How can I accomplish this? I've tried
return redirect(reverse('social:login'), args=('facebook',))
and
return redirect(reverse('social:login'), kwargs={'backend':fb})
(to get to /login/facebook) but I'm getting a NoReverseMatch
The Django URL system and RegExes are confusing me a bit =(
EDIT: All right, it looks like I was making a mess with these URLs.
A simple solution that works (thank you #Alasdair in the comments):
return redirect('social:begin', backend='facebook')

Not able to pass API urls to the REST API app

I have my Django project split into three apps:
frontend: You guessed it, the pure frontend.
shared_stuff: This is where I've put my models, because I feel they might be shared between apps later on.
rest_api: The, well, REST API.
All three apps are also registered in settings.py.
Now my problem is that the urls meant for rest_api app are also being serviced by the frontend app. Here's what my main urls.py looks like:
urlpatterns = [
url(r'^api/v1', include('rest_api.urls')),
url(r'^', include('frontend.urls')),
]
Am I doing something wrong? Please feel free to ask for more info!
Try this: url(r'^/api/v1', include('rest_api.urls')). Just add a front slash to the pattern.
When you visit http://host/api/v1/xxx, django will use /api/v1/xxx to match the patterns. But ^api/v1 fails the match because of the missing front slash.

Django REST framework - different views for same url depending on http method

I have a REST framework API and I have to dispatch an url to 2 different views, depending on a method.
the architecture is like this:
bookshop/authors/ - lists all authors, with POST - adds an author
bookshop/authors/<author>/ - with GET - gets details for an author, including books
bookshop/authors/<author>/ - with POST - creates a posting of a book for the same author.
bookshop/authors/<author>/<book>/ - gets a book, no posting
In general, for all my API I'm using Viewsets with Routers.
I tried doing this:
urlpatterns = patterns(
'',
url(r'^author/(?P<author>[0-9]+)',
AuthorViewSet.as_view({'get': 'retrieve'}),
name='author-detail'),
url(r'^author/(?P<author>[0-9]+)',
BookViewSet.as_view({'post': 'create'})),
)
but then it goes to the first url and the viewset checks for methods and throws an exception MethodNotAllowed.
I tried to catch it like this:
try:
urlpatterns = patterns(
'',
url(r'^author/(?P<author>[0-9]+)',
AuthorViewSet.as_view({'get': 'retrieve'}),
name='author-detail')
)
except MethodNotAllowed:
urlpatterns = patterns(
'',
url(r'^author/(?P<author>[0-9]+)',
BookViewSet.as_view({'post': 'create'})),
)
But it doesn't work too.
Is there any way to do it using viewsets?
The problem is that organizing your API in such a manner breaks RESTful convention. Breaking RESTful convention is not always bad, but usually represents a poor design and certainly means it is harder to user 3rd party software designed around the restframework to support your schema. So my suggestion is to update your API schema to:
GET bookshop/authors/ - lists all authors
POST bookshop/authors/ - creates an author
GET bookshop/authors/<author>/ - gets details for an author
POST bookshop/authors/<author>/books/ - creates a book for an author
GET bookshop/authors/<author>/books/<book> - gets a book
If you need to add postings you can also have (I'm not sure of the relationships between the objects so, not sure if the below accurately represents that relationship).
POST bookshop/authors/<author>/books/<book>/postings - creates a posting
GET bookshop/authors/<author>/books/<book>/postings/<posting> - gets a posting
When using Viewsets, typically you'll want to register your views with a router instead of binding views directly as you have done. The router will take care of "routing" the request to the correct handler.
Checkout:
http://www.django-rest-framework.org/api-guide/viewsets/#example
http://www.django-rest-framework.org/api-guide/routers/

How to document Django URLs with Sphinx?

I'm generating a new Django project, with several apps. The main goal for this project is to create a REST API. Right now, I've Sphinx working, creating documentation of all my project with
sphinx-quickstart
and
sphinx-apidoc -o doc/packages .
All works well, except for URLs. I wanted to have my URLs documented, as a nice API, fully integrated with the rest of Sphinx documentation.
Is it possible?
It is. This isn't directly documenting the URLs, but I connected the URLs to view documentation in this way:
Make a Sphinx extension that looks something like:
from django.core.urlresolvers import get_resolver
def setup(app):
app.connect('autodoc-process-docstring', process_django_view)
def process_django_view(app, what, name, obj, options, lines):
if what=='function':
res = get_resolver()
if res.reverse_dict.has_key(obj):
url_struct = res.reverse_dict[obj]
lines[:0] = [
"| URL structure: %s\n" % url_struct[0][0][0]
]
Then you need to add it to extensions in your conf.py and also load in the Django environment like explained here: How to build sphinx documentation for django project
There's some other stuff like the regex in url_struct and the parameters that you can include. My own also searches multiple URL resolvers that correspond to different domains.