Django writing view with sudomain - django

With my (Django v 1.17) project I am using django-subdomains.
I have no problem to call index view and when I open my url https://subdomain.domain.com I will get index.html.
My issue that I wrote a new view called example for the sub-domain but when I open the url https://subdomain.domain.com/exmaple I will get error Page not found (404).
Hete is my code:
settings.py
INSTALLED_APPS = [
'subdomain'
]
SUBDOMAIN_URLCONFS = {
'subdomain': 'subdomain.urls',
}
subdomain/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'^$example', views.example, name='example'),
]
subdomain/views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
def index(request):
template = loader.get_template('subdomain/index.html')
return HttpResponse(template.render())
def example(request):
template = loader.get_template('subdomain/example.html')
return HttpResponse(template.render())
Error:
Page not found (404)
Request Method: GET
Request URL: https://subdomain.domain.com/example
Using the URLconf defined in subdomain.urls, Django tried these URL patterns, in this order:
1. ^$ [name='index']
2. ^$example [name='example']
The current path, econ, didn't match any of these.
Please advise how to fix this issue and write view for sub-domain.

This is unrelated to django-subdomains. The dollar should be at the end of the regex.
url(r'^example$', views.example, name='example'),
The dollar matches the end of the string, so if you have it at the beginning then it's not going to match.

Related

Page Not Found 404 Django & Python

I am having the following error
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in Decoder.urls, Django tried these URL patterns, in this order:
form.html [name='form1']
hl7 [name='hl7']
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Its my first time writing code using Django
`from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('hl7rest.urls')),
]`
and this other file
from . import views
from django.urls import path
urlpatterns = [
path('form.html', views.render_form_View, name='form1'),
path('hl7', views.hl7_web_view ,name='hl7'),
]
Your paths don' t match the request.
You can create a TemplateView subclass for render your template:
Views
from django.views.generic.base import TemplateView
class HomePageView(TemplateView):
template_name = "display_form.html"
Url patterns
urlpatterns = [
path('', HomePageView.as_view(), name='home')
]

Page not found (404) on django

I am using django 3.2 to build a personal e-commerce project, Got Error-Page not found (404) on visiting my products page url Method:GET URL:http://127.0.0.1:8000/products Using the URLconf defined in main.urls. I have followed all the conventions and the other pages are all working but this one is giving me a[urls[\views][1][browser display] hard time as i feel everything is correct, i might be wrong, pls help and if u need to see more of my code, please let me know.
This is my code for urls
from django.urls import path
from django.views.generic.detail import DetailView
from django.views.generic import *
from main import models, views
app_name = 'main'
urlpatterns = [
path('', views.home, name='home'),
path('about_us/', views.about_us, name='about_us'),
path('contact-us/', views.ContactUsView.as_view(), name='contact_us'),
path(
"products/<slug:tag>/",
views.ProductListView.as_view(),
name="products"
),
path("product/<slug:slug>/", DetailView.as_view(model=models.Product), name='product'),
]
my code for views
from django.views.generic.edit import FormView
from .forms import ContactForm
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.shortcuts import get_object_or_404
from main import models
class ProductListView(ListView):
template_name = "main/product_list.html"
paginate_by = 4
def get_queryset(self):
tag = self.kwargs['tag']
self.tag = None
if tag != "all":
self.tag = get_object_or_404(
models.ProductTag, slug=tag
)
if self.tag:
products = models.Product.objects.active().filter(
tags=self.tag
)
else:
products = models.Product.objects.active()
return products.order_by("name")
then my browser
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products
Using the URLconf defined in booktime.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
about_us/ [name='about_us']
contact-us/ [name='contact_us']
products/<slug:tag>/ [name='products']
product/<slug:slug>/ [name='product']
^media/(?P<path>.*)$
The current path, products, didn’t match any of these.
You have to create a path to url ending with "products/". You create url just to sent with parameter "tags". "products/slug:tag/"
So, add 'products/' to your urls path the url below and you'll access with 'http://127.0.0.1:8000/products/'
path(
"products/",
views.ProductListView.as_view(),
name="products"
)

Page not found for the template returned by TemplateResponse in Django

I have an app which is called sitepages which have three models I wanna pass their objects to a html page called listing.html which is inside templates/sitepages
the sitepages urls.py contains:
from django.views.generic import TemplateView
from django.urls import include
from django.urls import path
from sitepages import views
app_name = "sitepages"
urlpatterns = [
path("news-events-listing/", views.view, name='listing'),
]
the sitepages/views.py:
from .models import Listing, Details1, Details2
from django.template.response import TemplateResponse
def view(request):
return TemplateResponse(request, 'sitepages/listing.html', {
'first_pages': Details1.objects.all(),
'seconde_pages': Details1.objects.all(),
'listing_page': Listing.objects.first(),
})
in the root urls.py I added:
path("", include("sitepages.urls", namespace='sitepages'))
when I put in any template the following:
Listing
it redirects me to the url /news-events-listing but no page is found and it gives me 404 error...what am I doing wrong? why the template is not returned? I should mention that I'm using wagtail for the whole site (I don't know if it's related)
In your root urls.py, make sure your new line
path("", include("sitepages.urls", namespace='sitepages'))
appears before the path("", include(wagtail_urls)) line. The wagtail_urls pattern matches any URL path and passes it to Wagtail to be handled as a Wagtail page, so any patterns after it will never be reached.

Python - Django add multiple urlpatterns for multiple views of template

I'm very very new to Python 3 and Django and I get to the following problem: I use a standard Template and now how to set it up when there is 1 view. But I don't get the code right for multiple views. I currently run the page locally
At the moment I have tried to change different orders within urlpatterns, and they do work when only 1 url in in there, but I can't get the second one in
views.py
from django.shortcuts import render, render_to_response
# Create your views here.
def index(request):
return render_to_response('index.html')
def store(request):
return render_to_response('store.html')
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^store/$', views.store, name='store'),
url(r'^admin/', admin.site.urls)
]
urlpatterns += staticfiles_urlpatterns()
I would like the url pattern that lets me go to the index view and the store view
EDIT:
Full code is shared via: https://github.com/lotwij/DjangoTemplate
The error in the comments shows you are going to http:/127.0.0.1:8000/store.html, but your URL pattern url(r'^store/$', ...) does not include the .html, so you should go to http:/127.0.0.1:8000/store/.
The Django URL system uncouples the URL from the name of the template (sometimes the view doesn't even render a template!). You could change the regex to r'^store.html$ if you really want .html in the URL, but I find the URL without the extension is cleaner.

django url not working when directions followed

This is driving me crazy. Everything looks good. I am getting this error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/home
Using the URLconf defined in gds.urls, Django tried these URL patterns, in this order:
^admin/
^fixiss/
The current URL, home, didn't match any of these.
Here is my root url:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^fixiss/', include('fixiss.urls')),
]
My app url:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name="index"),
]
And the view in my app:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Home page!")
Any help would be greatly appreciated!
Assuming your "app url" module is 'fixiss.urls' where you only have one pattern (the empty string) and you are you are including it under fixiss/, the only match should be:
http://localhost:8000/fixiss/
If you change your one pattern to:
url(r'^home$', views.home, name="index")
that view will be served under
http://localhost:8000/fixiss/home/
The actual name of the view function (home in this case) is rather irrelevant when it comes to url pattern matching. What counts is the specified regex pattern.
This is very well documented:
Django url dispatching in general
Including urls in particular
That is because no url matches home/. Your url should be http://localhost:8000/fixiss/
In the app's (fixiss) url file, the regex is empty meaning, it does not expect a string after fixiss/ in the url for it to match.