Can someone help me in figuring out where I went wrong?
Project URLs
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('trips/',include('trips.urls')),
path('customers/',include('customers.urls')),
path('drivers/',include('drivers.urls')),
path('vehicles/',include('vehicles.urls')),
path('admin/', admin.site.urls),
]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
App URLs
from django.urls import path
from . import views
urlpatterns = [
path('',views.customers, name='customers'),
path('<int:pk>/', views.customer_details, name='customer_details'),
]
App View
from django.shortcuts import render, get_object_or_404
from .models import *
# Create your views here.
def customers(request):
customers = Customer.objects.all().order_by('id')
return render(request, "customers.html", {'customers': customers, 'custactive': "active"})
def customer_details(request, pk):
customer = get_object_or_404(Customer, pk=pk)
return render(request, "/customer_details.html", {'customer': customer})
How I'm calling the page
<td>{{customer.name}}</td>
I'm hoping to render the template at localhost/customers/id
Help is really appreciated.
In your function customer_details you return :
return render(request, "/customer_details.html", {'customer': customer})
You have to return "customer_details.html" without the / :
return render(request, "customer_details.html", {'customer': customer})
Related
After filling the page should appear 'done', but i have an error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/candidate/done.html
The current path, candidate/done.html, didn't match any of these.
Can't configure redirect to 'done' page.
Here views.py:
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import AnketaForm
from .models import Anketa
def anketa_create_view(request):
if request.method == 'POST':
form = AnketaForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('candidate/done.html')
else:
form = AnketaForm()
return render(request, 'candidate/anketa_create.html', {'form': form})
urls.py (apps/candidate)
from django.urls import path
from . import views
urlpatterns = [
path('', views.anketa_create_view, name = 'anketa_create_view'),
]
urls.py
from django.contrib import admin
from django.urls import path, include
from candidate.views import anketa_create_view
urlpatterns = [
path('done/', anketa_create_view),
path('', anketa_create_view),
path('grappelli/', include('grappelli.urls')),
path('admin/', admin.site.urls),
]
you need to give a name to the done url
path('done/', anketa_create_view, name='done'),
and can do with reverse
return HttpResponseRedirect(reverse('done'))
or you can do redirect shortcut
return redirect('done')
Remove in urls.py
path('done/', anketa_create_view)
and replace in views.py
return HttpResponseRedirect('candidate/done.html')
to
return render(request, 'candidate/done.html', {})
there is a problem with a url i've created in django that it doesn't totally work
this is urls.py
from django.conf.urls import include, url
from django.contrib import admin
from pizza import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('', views.home,name='home'),
url('order/', views.order,name='order'),
]
and this is views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Home page")
def order(request):
return HttpResponse("Order a pizza page")
That is incorrect syntax, try using path('') instead of url
If pizza it's your app you must use:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('pizza/', include('pizza.urls')),
]
in urls.py and add your app in settings.py inside INSTALLED_APPS
OR
If when you call from pizza import views is the name of Project you should import as import .views only
Update in project urls.py file.
i have python 3.6 and django 2.0 installed. i keep having this same error...Type error:View must be a callable or a list/tuple in the case of include() in django 2.0
my app urls the app's name is catalog
from django.urls import path
from django import views
urlpatterns = [
path('index/',
views.catalog_home, name='catalog_home'),
path('category/<category_slug>/',
views.show_category, name='show_category'),
path('product/<product_slug>/',
views.show_product, name='show_product')
]
my project urls this is the projects urls
from django.contrib import admin
from django.urls import path, include
from django.views import static
urlpatterns = [
path('admin/', admin.site.urls),
path('catalog/', include('catalog.urls'),),
path('static/', static,
{
'document_root': 'C:/Users/USER1/PycharmProjects/untitled13/static'
}),
]
my views
from django.shortcuts import get_object_or_404, render_to_response
from catalog.models import Category, Product
from django.template import RequestContext
def index(request, template_name='catalog/index.html'):
page_title = 'online shop for all items'
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))
def show_category(request, category_slug, template_name='catalog/category.html'):
c = get_object_or_404(Category, slug=category_slug)
products = c.product_set.all()
page_title = c.name
meta_keywords = c.meta_keywords
meta_description = c.meta_description
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))
def show_product(request, product_slug, template_name='catalog/product.html'):
p = get_object_or_404(Product, slug=product_slug)
categories = p.categories.filter(is_active=True)
page_title = p.name
meta_keywords = p.meta_keywords
meta_description = p.meta_description
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))
I think you missed the actual reference of static. Change static to static.serve in urlpatterns
from django.contrib import admin
from django.urls import path, include
from django.views import static
urlpatterns = [
path('admin/', admin.site.urls),
path('catalog/', include('catalog.urls'), ),
path('static/', static.serve,
{
'document_root': 'C:/Users/USER1/PycharmProjects/untitled13/static'
}),
]
Reference : Serving files in development
I am having trouble with my urls. I have one app called users that has two models, Salon and Stylist. The url /stylists and /salons is rendering the same view (stylist_results) however /salons should render salon_results and I cannot figure out why. I think there may be something wrong with the way I am using namespaces.
users/urls.py
from django.conf.urls import url
#import views from the current directory
from . import views
urlpatterns=[
url(r'^$', views.stylist_results, name='stylist_results'),
url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
url(r'^$', views.salon_results, name='salon_results'),
url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]
users/views.py
from django.shortcuts import get_object_or_404, render
from .models import Salon, Stylist
# Create your views here.
def salon_results(request):
salons = Salon.objects.all()
return render(request, 'salons/salon_results.html', {'salons': salons})
def salon_detail(request, pk):
salon = get_object_or_404(Salon, pk=pk)
return render(request, 'salons/salon_detail.html', {'salon': salon})
def stylist_results(request):
stylists = Stylist.objects.all()
return render(request, 'stylists/stylist_results.html', {'stylists': stylists})
def stylist_detail(request, pk):
stylist = get_object_or_404(Stylist, pk=pk)
return render(request, 'stylists/stylist_detail.html', {'stylist': stylist})
urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
url(r'^salons/', include('users.urls', namespace='salons')),
url(r'^stylists/', include('users.urls', namespace='stylists')),
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
]
urlpatterns += staticfiles_urlpatterns()
views.py
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Split your stylist and salon views into two separate modules. You might consider creating two different apps, but you don't have to.
users/stylist_urls.py
urlpatterns = [
url(r'^$', views.stylist_results, name='stylist_results'),
url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
]
users/salon_urls.py
urlpatterns = [
url(r'^$', views.salon_results, name='salon_results'),
url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]
Then update your project's urls.py with the new modules:
urlpatterns = [
url(r'^salons/', include('users.salon_urls', namespace='salons')),
url(r'^stylists/', include('users.stylist_urls', namespace='stylists')),
...
]
At the moment, the regexes for your salon URLs are exactly the same as the stylist URLs, so the salon URLs will always match first.
You are specifying same set of url's(users.urls) for salons and stylists here:
url(r'^salons/', include('users.urls', namespace='salons')),
url(r'^stylists/', include('users.urls', namespace='stylists')),
What did you expect to happen
You are including same users/urls.py in urls.py
It does following:
find me /stylist/ => go into included urls
find first occurance of url(r'^$', views.stylist_results, name='stylist_results'),
renders that view
same thing happens with /salons/
URL Dispatcher documentation
I am new in Python, I have a problem with the admin URL.
When I type
localhost:8000/admin/
My browser tells me:
DoesNotExist at /admin/
Question matching query does not exist
My index page and detail pages work fine.
I thought it was a mistake in the order of URL's,
I changed the order of the admin and detail URL,
but still nothing.
Can somebody please give me a hint.
project/urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'books.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<book_title>[\w_-]+)/$', 'books.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
books/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all
context = {'latest_question_list': latest_question_list}
return render(request, 'books/index.html', context)
def detail(request, book_title):
question = Question.objects.get(title=book_title)
return render(request, 'books/detail.html', {'question': question})
I forgot to put parentheses for Question.objects.all So silly of me.
add this before urlpatterns in urls.py
admin.autodiscover()