Page not found (404 ) - python

Error is :
Page not found (404)
Request Method: GET
Request URL: 'http://127.0.0.1:8000/admin/'
Using the URLconf defined in orangeowl.urls, Django tried these URL patterns, in this order:
^product/$
^product/(?P<slug>[-\w]+)/$
The current URL, admin/, didn't match any of these.
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.defaults import *
from django.contrib import admin
from website.models import Product
from website.views import index
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', admin.site.urls), #Lets us access the admin page
(r'^$', 'orangeowl.website.views.index'),
)
urlpatterns = patterns('django.views.generic.list_detail',
url(r'^product/$', 'object_list',
{'queryset': Product.objects.all()}),
url(r'^product/(?P<slug>[-\w]+)/$', 'object_detail',
{'queryset': Product.objects.all()})
)
views.py
from models import Catalog
from models import Product
from models import CatalogCategory
from models import ProductAttribute
from models import ProductDetail
from django.http import HttpResponse
from django.shortcuts import render_to_response
def index(request): #Define our function, accept a request
catalog = Catalog.objects.all()
catalogcategory = CatalogCategory.objects.all()
product = Product.objects.all()
productattribute = ProductAttribute.objects.all()
productdetail = ProductDetail.objects.all()
return render_to_response('index.html', {'catalog': catalog}, {'product':product})
return render_to_response('product_detail.html', {'productdetail': productdetail})
return render_to_response('product_list.html', {'productattribute': productattribute})
settings.py
ROOT_URL_CONF = project.urls
INSTALLED_APPS = project.appname

Notice that you are defining urlpatterns twice. That's why admin's is not included. You can do like this:
urlpatterns = patterns('', (r'^admin/', admin.site.urls), #Lets us access the admin page
(r'^$', 'orangeowl.website.views.index'),
)
urlpatterns += patterns('django.views.generic.list_detail',
url(r'^product/$', 'object_list',
{'queryset': Product.objects.all()}),
url(r'^product/(?P[-\w]+)/$', 'object_detail',
{'queryset': Product.objects.all()})
)
Hope this helps!

Related

Python URLs is showing an error, "The empty path didn't match any of these"

Using the URLconf defined in todo.urls, Django tried these URL patterns, in this order:
^admin/
^todoapp/
The empty path didn't match any of these.
404 error is showing when I run my code which you can find below. This code is app url:
from django.contrib import admin
from django.conf.urls import url
from todoapp import views
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^$',views.index,name='index'),
#todoapp/1
url(r'^(?P<task_id>[0-9]+)/$',views.detail, name='detail'),
]
And it is the main url.py
from django.contrib import admin
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^todoapp/',include('todoapp.urls')),
]
views.py is here:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Task
from django.template import loader
# Create your views here.
def index(request):
task_list = Task.objects.all()
template = loader.get_template('todoapp/index.html')
context = {
'task_list': task_list
}
return render(request, 'todoapp/index.html',context)
def detail(request, task_id):
task = Task.objects.get(pk=task_id)
context = {
'task': task,
}
return render(request,'todoapp/detail.html',context)
Why it is not working, I could not find a problem. How can I fix it?

how to pass variables in the urls in django?

i tried to pass variables in url in django but it keeps getting 404 error
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from test_url import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^detail/<int:id>/',views.detail)
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def detail(request,id):
return HttpResponse("<h1>{}.</h1>".format(i

Django After login succeed, redirect again to login page

My Django project has 5 apps: app1, app2 ......
each app has one template index.html and 1 view named index.
project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('main.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
app1/urls.py
from django.conf.urls import url, include
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^accounts/login/$', auth_views.login, name='login'),
app1/views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, user_passes_test
def is_member(user):
return user.groups.filter(name='group1').exists()
#login_required
#user_passes_test(is_member)
def index(request):
getting our template
template = loader.get_template('app1/index.html')
rendering the template in HttpResponse
return HttpResponse(template.render())
setting.py
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
The 5 apps requirer login based on group
app one I have no problem to login but the reset of the apps after login succeed, I will be redirected to login page again.
The reset of the apps has exactly the same code except the group name and template location.
Also the logger result show me that there is no problem with credential.
I tried to change the group nae for other app as app1 and still I am getting same result.
Please advice me how to solve this problem.

Django - URL routing issues (cannot import name 'urls')

I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.
polls/urls.py:
from django.conf.urls import patterns, urls
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and finally, the index method in views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1>Hello world!</h1>");
I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!
The problem is in your import statement - there is no urls function in django.conf.urls package.
Replace:
from django.conf.urls import patterns, urls
with:
from django.conf.urls import patterns, url

Created pages/subpages in Django

I am currently learning Django and I am trying to create a few pages and subpages on my site.
project
urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/', include('home.urls', namespace = 'home')),
url(r'about/', include('about.views', namespace = 'About_page')),
)
I first create a home page:
urls.py:
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
views.py:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
def index(request):
return render(request,'homepage_template/home.html')
Next I tried to create an about page but I get this error:
Exception Type: ImproperlyConfigured
Exception Value:
The included urlconf <module 'about.views' from '/home/bradford/Development/Django/pub_pic/about/views.pyc'> doesn't have any patterns in it
This is what my about app looks like:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
def index(request):
return render(request,'About_template/about.html')
I don't have a urls.py but I thought the line
url(r'about/', include('about.views', namespace = 'About_page')),
would directly include the about.views. However I was wrong because this never called my index() function in about/views.py
I later changed the url() in pub_pic/urls.py to:
url(r'about/', include('about.views.index', namespace = 'About_page')),
But I got this error:
Exception Value:
No module named index
I think this caused due to the fact that only about.views is a module, not index()
I'm not quite sure how to create pages or subpages and best practices for a well structured project. Could someone give me some suggestions please? Thank you!
In your project urls.py, you cannot include an application's view files in urlpatterns.
url(r'about/', include('about.urls', namespace = 'About_page')), )
And in about/urls.py
from django.conf.urls import patterns, url
from about import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),)
If there is only one url in about/urls.py, then you can directly put it in urls.py of project.
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/', include('home.urls', namespace = 'home')),
url(r'^about/$', 'about.views.index', name="About_page_index"),
)

Categories

Resources