what's wrong with my urls.py in this example? - django

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')

You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)

the issue was I forgot to include the request in the return render.

The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')

Related

TypeError: view must be a callable or a list/tuple in the case of include()

I am new to django and python. During url mapping to views i am getting following error:
TypeError: view must be a callable or a list/tuple in the case of include().
Urls. py code:-
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
] # is a function in view.
views.py code:-
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
#function based views
def post_home(request):
response = "<h1>Success</h1>"
return HttpResponse(response)
Traceback
In 1.10, you can no longer pass import paths to url(), you need to pass the actual view function:
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
Replace your admin url pattern with this
url(r'^admin/', include(admin.site.urls))
So your urls.py becomes :
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
]
admin urls are callable by include (before 1.9).
For Django 1.11.2
In the main urls.py write :
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include("Post.urls")),
]
And in the appname/urls.py file write:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.post_home),
]
Answer is in project-dir/urls.py
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
Just to complement the answer from #knbk, we could use the template below:
as is in 1.9:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls), #it's not allowed to use the include() in the admin.urls
url(r'^posts/$', include(posts.views.post_home),
]
as should be in 1.10:
from your_project_django.your_app_django.view import name_of_your_view
urlpatterns = [
...
url(r'^name_of_the_view/$', name_of_the_view),
]
Remember to create in your_app_django >> views.py the function to render your view.
You need to pass actual view function
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
This works fine!
You can have a read at URL Dispatcher Django
and here Common Reguler Expressions Django URLs

NoReverseMatch at /new_application/check_login/

I am new to DJango. I am getting error 'NoReverseMatch at /new_application/check_login/' while redirecting view from another view.
different files listed below.
Main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^new_application/', include('new_application.urls')),
]
views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Login
...
def check_login(request):
...
return HttpResponseRedirect(reverse('new_application:loggedin',args=(user,)))
def loggedin(request, user):
return render(request, 'new_application/loggedin.html',{'full_name': user.first_name +" "+ user.last_name})
Application urls.py
from django.conf.urls import url
from . import views
app_name = 'new_application'
urlpatterns = [
url(r'^$', views.login, name='login'),
url(r'^check_login/$', views.check_login, name='check_login'),
url(r'^loggedin/$', views.loggedin, name='loggedin'),
url(r'^invalid_login/$', views.invalid_login, name='invalid_login'),
url(r'^logout/$', views.logout, name='logout'),
]
Here is an error image : error image
please give the solution for fixing this error.
Thank You.
The regex pattern must match against the given url...
url(r'^loggedin/(?P<user>\w+)/$', views.loggedin, name='loggedin'),
Reverse should match one of URL names from urls.py
return HttpResponseRedirect(reverse('loggedin',args=(user,)))

Django Page not found (404) error with url config

I have a problem with urls config in my Django project, in my root url config i have this:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('main.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^profile/', include('accounts.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
And in my app url config i have this:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'main.view.home', name='main_home'),
url(r'^integrations/$', 'main.view.integrations', name='main_integrations'),
)
But when I go to render this url http://127.0.0.1:8000/integrations/ I obtain an error 404 Not Found.
Thanks by yours answers.
Yes, my app named 'main' and this is my view code:
def home(request):
user = request.user
if not user.is_authenticated():
data = {
'messages': get_messages(request),
}
return render_to_response('home.html', data, context_instance=RequestContext(request))
data = {
'messages': get_messages(request),
}
return render_to_response('home_user.html', data, context_instance=RequestContext(request))
def integrations(request):
return render_to_response('/main/integrations.html', context_instance=RequestContext(request))

Redirect to custom 404 html page in DjangoCMS

How can I redirect the user to my custom 404.html page if he enters a wrong path ?
views.py:
from django.shortcuts import render, render_to_response, redirect
from django.template import RequestContext
import threading
def url_redirect(request):
return render_to_response('blog.html', RequestContext(request))
def post_redirect(request):
return render_to_response('post.html', RequestContext(request))
def privacy_redirect(request):
return render_to_response('privacy.html', RequestContext(request))
def terms_conditions_redirect(request):
return render_to_response('terms.html', RequestContext(request))
def page_404(request):
return render_to_response('404.html', RequestContext(request))
urls.py:
from __future__ import print_function
from cms.sitemaps import CMSSitemap
from django.conf.urls import *
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^blog', "website.views.url_redirect", name="url-redirect"),
url(r'^post', "website.views.post_redirect", name="post-redirect"),
url(r'^privacy', "website.views.privacy_redirect", name="privacy-redirect"),
url(r'^terms', "website.views.terms_conditions_redirect", name="terms_conditions-redirect"),
url(r'^404', "website.views.page_404", name="page_404"),
url(r'^', include('cms.urls')),
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns
I also have TEMPLATE_DEBUG = True and DEBUG = False in my settings.py file. Any help, please ?
Also, can you guys tell me if those methods from views.py are ok or if they could be rewritten in a better way ?
Thanks
add 404.html near base.html (main folder templates)
and when there is an error 404 , Django itself take your 404 page
in settings.py
DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['yourweb.com']

No module named django.contrib.auth when using things that redirect

I get the ImportError "No module named django.contrib.auth" both when I try to use the django.shortcuts redirect function and when I try to use:
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
I figure it can't be a coincidence that the only place I'm hitting this error is in places where the page is redirected, but maybe it is. I know that the user isn't actually getting logged out, so the error happens before you even get to any redirect code.
Below is my urls.py file.
import django.contrib.auth.views
from django.conf.urls.defaults import *
import django.contrib.auth
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('karma.views',
(r'^$', 'homepage'),
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
(r"^opportunities/nearby$", 'draw_map'),
(r'^admin/', include(admin.site.urls)),
url(r'', include('social_auth.urls')),
(r'^profile/', include('karmup.profile.urls')),
)
You are mixing up URL prefixes in your urlpatterns.
urlpatterns = patterns('karma.views',
(r'^$', 'homepage'),
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
(r"^opportunities/nearby$", 'draw_map'),
)
Django tries to find views relative to the given URL prefix, in your case 'karma.views'. Inside this module, there is no 'django.contrib.auth.views.logout', hence you get the ImportError.
Move the logout URL to a second block, e.g.:
urlpatterns += patterns('',
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
)
That should resolve your issue.