Django Oscar change URL pattern - django

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')),

Related

how to make subdomains in 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.

Hello. I am having a problem to change the pages in my django project that it shows a page not found (404)

I've been building my first Django project, and I was doing the "login page", and it is working, but when then, I've made a condition that if the login is wrong, it comes back to the login page and shows an error message, and if it is right, it should go to a page where is written "logadissimo", but when I try this last one, I get the problem below:
Page not found (404) Request Method: GET Request
URL: http://localhost:8000/ Using the URLconf defined in sitetcc.urls,
Django tried these URL patterns, in this order:
admin/ login/ login/submit login/logado 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.
this is my url.py from the project:
"""
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls.conf import include
from django.urls import path
from core import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login_user),
path('login/submit', views.submit_login),
path('login/logado', views.logado)
]
the urls.py from the application:
from . import views
from django.urls import path
app_name = 'core'
urlpatterns = [
path('', views.login_user, name='login'),
path('', views.submit_login, name='submit'),
path('', views.logado, name='logado')
]
the views.py:
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth import authenticate, login
from django.contrib import messages
# Create your views here.
def logado(request):
return render(request, 'logado.html')
def login_user(request):
return render(request,'login.html')
#csrf_protect
def submit_login(request):
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
print(username)
print(password)
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect('/logado/')
else:
messages.error(request,'Usuário e senha inválido. Favor tentar novamente:')
return redirect('/login/')
and a print screen with the folders' structure and the page "logado":
https://imgur.com/a/xAsBRHO
Thank you!
try return redirect('logado') and return redirect('login') syntax return redirect('your_view_name')
That's because you're passing a hardcoded URL (/logado/) to redirect that doesn't exists in your project's url.py, so Django can't match that route.
There are a couple of ways you can pass an argument to redirect, you can see some examples here. I strongly recommend you to use the name of your url's view, so in the future you can change the route of your path without any issues.
Also, you can declare your URLs like this, in that way you'll keep things clean and not bloat your project's urls.py.
Doing this you'll have your project's urls.py like this:
from django.contrib import admin
from django.urls.conf import include
from django.urls import path
from core import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', include('core.urls'),
]
And your application's urls.py like this:
from . import views
from django.urls import path
app_name = 'core'
urlpatterns = [
path('', views.login_user, name='login'),
path('login/submit/', views.submit_login, name='submit'),
path('login/logado/', views.logado, name='logado')
]
(Don't forget the trailing / at the end of each route)
Now you can call your view like this return redirect('core:logado')

no module named search after adding project url

I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.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.