how to make subdomains in django? - django

I want to make a panel which has two main urls by the name user and admin and also each of the "user" and "admin" urls have other urls. for example -> " site.com/panel/user/id/profile " OR " site.com/panel/admin/id/addItem " . but idk how can I make this main urls and subdomain urls :) ->
ecoverde/urls.py :
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('admin/', admin.site.urls),
path('', include('base.urls')),
path('panel/', include('panel.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
ecoverde/panel/urls.py ((this is where i want to add two main urls and other sub urls) :
from .views import userPanel, Compare, saves
from django.urls import path
urlpatterns = [
#main urls that i want
path('user dashboard/<str:pk>', userPanel, name='userPanel'),
path('admin dashboard/<str:pk>', adminPanel, name='adminPanel'),
#sub urls that i want
path('compare', Compare, name='compare'),
path('saves', saves, name='saves'),
#...
]

If you are using class-based views, inside of the view you add the function that you would like to accompany your sub-domain. For example, in userPanel view, if you want to have the subdomain /userPanel/validate/, by using djangos action decoration from rest_framework.decorators import action, the function could look like this #action(methods=['get'], detail=False, url_path="validate") def validate(self, request): your function
url_path defaults to your function name if not specified.

Related

onw of my two app is not working in django

Below is my code. In my hello_world project there is two app pages. one is home page and another is profile page. home page is working fine, but profile page is showing error.
hello_world urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home_page.urls',)),
path('profile_page',include('profile_page.urls',))
]
home page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home,name='home page'),
]
home page views.py
from django.http import HttpResponse
def home(request):
return HttpResponse('home page')
profile page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('profile_page',views.profile,name='profile page'),
]
profile page views.py
from django.http import HttpResponse
def profile(request):
return HttpResponse('profile page')
You need to use redirect method and include view name space as an argument..
Find the updated code:
from django.http import HttpResponse
def profile(request):
return redirect('profile page')
Don't forgot to import redirect...
The first argument of the path() function, i.e. the 'route' argument, must end with a forward slash.
path('profile_page/',include('profile_page.urls',))
Notice the forward slash at the end of the first argument, 'profile_page/'.
Your URLconf is not matching the expected pattern because of the missing slash.

Django not getting path not matching

I am trying to make a certain url on my localhost show "Alex!" when it is entered in. For some reason I am getting this error: "Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, sauce, didn’t match any of these."
The app name is "main"
Here is my code:
main.urls
from django.urls import path
from . import views
urlpatterns = [
path('sauce/', views.index, name='index'),
]
main.views
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Alex!")
mysite.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('sauce/', include('main.urls')),
path('admin/', admin.site.urls),
]
I also tried doing this but with the homepage and not "sauce/" but that didn't work either.
Your path is not matching because you have "sauce" in both main.urls and mysite.urls. When you use include the urls from main.urls will be appended to the path you specify in mysite.urls, so the combined url becomes:
http://127.0.0.1:8000/sauce/sauce
I would replace the "sauce/" in mysite.urls to just "".

Django Oscar change URL pattern

I have setup a django-oscar project and I'm trying to configure the URLs. My goal is to change /catalogue to /catalog.
As per the documentation I've added app.py in myproject/app.py
myproject/app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urlpatterns = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
return urlpatterns
application = MyShop()
myproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from .app import application
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
url(r'^index/$',views.index, name = 'index'),
]
The project server runs without any error, but when I try localhost:8000/catalog I get
NoReverseMatch at /catalog/ 'customer' is not a registered namespace.
The expected output is localhost:8000/catalog should return the catalogue page.
You can try this
in app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urls = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
urls = urls + super(MyShop,self).get_urls()
return urls
application = MyShop()
And in your urls.py
you can simply add this
from myproject.app import application as shop
url(r'', shop.urls),
Hope it help for you
Expanding on c.grey's answer to specify how to replace instead of add the urls -
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
def get_urls(self):
urls = super(MyShop, self).get_urls()
for index, u in enumerate(urls):
if u.regex.pattern == r'^catalogue/':
urls[index] = url(r'^catalog/', include(self.catalogue_app.urls))
break
return urls
application = MyShop()
You need to include the URLs, not reference them directly.
url(r'', include('application.urls')),

Django Rest Framework URL Mapping for multiple apps

I have a django-rest project called main and under it I have created an app called users. So, my project has the files :-
main/main/urls.py
and
main/users/urls.py
In users/urls.py I have
from django.conf.urls import url, include
from rest_framework import routers
from users import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
and in the main/main/urls.py I have
from django.conf.urls import url
from django.contrib import admin
from users import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users.urls),
]
However, I keep getting the error NameError: name 'users' is not defined. What is the correct way to set up urls when I have multiple apps? I would like to have a urls.py file for each app that is independent of the project. And in the root urls.py would include routing to different apps.
You import url not user, can try it
from users import urls as users_url
# ^^^^^^^^
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', users_url),
# ^^^^^^^
]
but better:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^users/', include('users.url')),
# ^^^^^^^
]
more details including-other-urlconfs

Django URL not working as expected

I have a URL that does not work, for some reason. I get a 404, "'new' could not be found". Here is my urls.py:
url(r'^assets/new', 'watershed.views.new_asset', name='new_asset'),
There is a lot more in my urls.py but this is the ONLY one that contains the word, "assets" in it. If I change this url to anything/new, it works. If i misspell assets (assettss/new), it works. If I take out the /new and just use "assets", it also works fine. In my views folder I have an __ init __.py which contains the following:
from groups import *
from members import *
from leave_group import *
from payments import *
from assets import *
I also have an assets.py, which contains the following:
from django.contrib.auth.decorators import login_required
from watershed.models import Member, Org, OrgToMember, Asset
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
def new_asset(request):
return render(request, 'asset_add.html')
I have no idea what Django does not like about assets/new.
UPDATE: Here is my full urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^', include('outside.urls')),
url(r'^blog', include('blog.urls')),
url(r'^admin', include(admin.site.urls)),
url(r'^logout', 'watershed.views.logout', name='logout'),
url(r'^register/create', 'watershed.views.create', name='create'),
url(r'^register', 'watershed.views.register', name='register'),
url(r'^translog/(\d+)', 'watershed.views.translog', name='translog'),
url(r'^settings', 'watershed.views.settings', name='settings'),
# Group URIs
url(r'^groups/(\d+)/leave', 'watershed.views.leave_group', name='leave_group'),
url(r'^groups/(\d+)/dissolve', 'watershed.views.dissolve_group', name='dissolve_group'),
url(r'^groups/new', 'watershed.views.add_group_form', name='add_group_form'),
url(r'^groups/(\d+)', 'watershed.views.dashboard', name='dashboard'),
url(r'^groups/add', 'watershed.views.add_group', name='add_group'),
url(r'^groups', 'watershed.views.groups', name='groups'),
# Member URIs
url(r'^members/(\d+)', 'watershed.views.profile', name='profile'),
url(r'^member/login', 'watershed.views.login', name='login'),
# Payments URIs
url(r'^payments', 'watershed.views.payments', name='payments'),
# Asset URIs
url(r'^assets/new', 'watershed.views.new_asset', name='new_asset'),
You new_assets function containts in assets.py file, buy you import this function from views.py file. Use this:
url(r'^assets/new', 'path.to.assets.new_asset', name='new_asset'),
I figured it out. The problem is that my static url in my settings.py is - wait for it:
STATIC_URL = '/assets/'
So, clearly, one of those must change.