Integrate Django Oauth Toolkit urls correctly - django

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

Related

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.

Undetected Django app when installing from git

I'm having trouble installing django-admin_action_mail from git.
I tried to install it via:
pip install
git+https://github.com/mjbrownie/django-admin_action_mail.git
But Django did not pick it up when I added it to settings.INSTALLED_APPS.
Did I miss something?
The admin code for that app is commented out (see here: https://github.com/mjbrownie/django-admin_action_mail/blob/master/admin_action_mail/admin.py ) so nothing is going to show up on the admin page - even if it's working and enabled.
It looks as though you need to create your own models to handle the mailing functions. Take a look at the README where it tells you to add something like the following in your app's admin.py:
from admin_action_mail.actions import mail_action
class MyModelAdmin(admin.ModelAdmin):
#Note all args are optional
actions = [
mail_action(
'description' : "Send Email to Related Users",
'email_dot_path' : 'email', # dot path string to email field (eg 'user.email')
'email_template_html' : 'admin_action_email/email.html'
'reply_to' : 'noreply#example.com' # defaults to request.user.email
)
]
admin.site.register(MyModel, MyModelAdmin)
Have you added a model like that to your own app's admin.py?
EDIT: As the problem appears to be with installation, the following should help:
You can add arbitrary paths to your wsgi path spec, that means it will pick up Python app modules in other locations. Assuming your app is installed in /home/user2161049/myapp you can put your external modules under /home/user2161049/myapp/external. In this case copy the contents of that app into /home/user2161049/myapp/external/admin_action_mail/.
To add this to your settings.py:
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(SITE_ROOT, 'external'))
The first line defines SITE_ROOT based on the current running script (setup.py) at startup. The second adds the external folder into the search path. You can put anything you want in there, and even define a specific folder somewhere else if you want to keep your externals out of your app folder. Restart the server and it should find the app just fine.

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.

Right way to handle HttpResponseRedirect

I have a Django app I'm trying to deploy. The Apache setting is configured in the way that i access my wsgi app by the following URL:
sitename.com/~amartino
Meaning I have only a wsgi.py file in my public_html directory.
I access my Django site via URL:
sitename.com/~amartino/expofit
For that's the way its been set in urls.py.
urlpatterns = patterns('',
('/param_select/$',session_check(param_select)),
('registration/$',registration),
('result_show/(\d+)',session_check(result_show)),
('^expofit/$',media_clean(start)),
('result_pick/$',session_check(result_pick)),
('mail_report/$',session_check(mail_report)),
('notification/$',session_check(notification)),
However, the problem I'm getting (which didn't show up in development :) ) is that I'm using a hardcoded HttpResponseRedirect in views.py.
...
#If all fields are valid
return HttpResponseRedirect('/expofit/param_select/')
#else reload page
...
Since the production environment doesn't place my site in the root of the URL, i'm having errors now because the upper HttpResponseRedirect translates to
sitename.com/expofit/param_select/
which isn't recognized by Apache.
I suppose I could remove the slash and have:
return HttpResponseRedirect('expofit/param_select/')
which would result in:
sitename.com/~amartino/expofit/registration/expofit/param_select/
but that doesn't seem the right way to do it for I would end up with a huge URL in no time.
Where is the design/configuration flaw here?
"the problem I'm getting that I'm using a hardcoded HttpResponseRedirect"
Well, don't do that then. That's why Django provides the reverse function, which takes your url name and calculates the proper absolute URL.

Django Blog App urls functionality

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