I can't seem to get the urls to match in oscar. My oscar app is a subapp in my main app. My understanding is to create views and urls in the app.py in the sub modules in oscar.It keeps throwing NoReverseMatch. Oscar pages all loads, just not my custom views
in myapp/shop/dashboard/app.py
from django.contrib.auth import views as auth_views
from django.contrib.auth.forms import AuthenticationForm
from django.conf.urls import include, url
from oscar.core.application import DashboardApplication
from oscar.core.loading import get_class
from views import HomeViews
class DashboardApplication(DashboardApplication):
def get_urls(self):
urls = [
url(r'^someurl/', HomeViews.dosomething, name="hi"),
]
return self.post_process_urls(urls)
application = DashboardApplication()
in myapp/shop/dashboard/views.py
from django.conf import settings
from django.db.models import User
from oscar.app.partner.models import Partner
from django.middleware.csrf import get_token
from oscar.apps.dashboard.views import HomeView as CoreHomeView
class HomeView(CoreHomeView):
def dosomething(request):
return HttpResponse("hello")
in my main app, I registered the shop in my settings.py
INSTALLED_APPS =[
...
]] + get_core_apps(['shop'])
and in my main app url I have included oscar urls
from shop.app import application as shop_app
urlpatterns = i18n_patterns(
url(r'^shop/', include(shop_app.urls)),
You need to extend the existing urls list, not replace it. That is mostly why you get no reverse match errors because the default urls that oscar might have, don't exist anymore. For any other non-oscar-forked app, this would have been okay (just your own urls and nothing else).
class DashboardApplication(DashboardApplication):
def get_urls(self):
my_urls = [
url(r'^someurl/', HomeViews.dosomething, name="hi"),
]
default_urls = super(DashboardApplication, self).get_urls()
return self.post_process_urls(my_urls) + default_urls
Related
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 developed a django project named "dms" and an app which name is "add_excel" last month. The app receieves excel files from web page and store the data into mysql database. Today I added another two apps, "add_struc" and "homepage", which should be another function app and the homepage app. But something odd shows up. After I clicked the "upload" button in "add_excel" app, instead of it's original functions, it redirects to "homepage" without doing anything to the database.
The VS Code shows:
[18/Apr/2019 11:08:00] "GET / HTTP/1.1" 200 317 # I opened the
homepage
[18/Apr/2019 11:08:02] "GET /addexcel/index/ HTTP/1.1" 200 1341 # I clicked to the "add_excel" app hyperlink
[18/Apr/2019 11:08:20] "POST /homepage/index/ HTTP/1.1" 200 317 #
I clicked "upload" but it redirected me to homepage again.
If I delete the homepage url in the urls.py for the whole project, and click the upload button again, it says:
Page not found (404) Request Method: POST Request
URL: http://127.0.0.1:8000/homepage/index/ Using the URLconf defined
in dms.urls, Django tried these URL patterns, in this order:
^admin/ ^$ [name='index'] ^addexcel/ ^addstruc/ The current path,
homepage/index/, didn't match any of these.
The urls.py in dms project:
from django.contrib import admin
from django.conf.urls import url, include
from homepage.views import index # default homepage
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', index, name='index'), # homepage
url(r'^addexcel/', include('add_excel.urls')),
url(r'^addstruc/', include('add_struc.urls')),
]
urls.py in add_excel app:
from django.conf.urls import url
from django.urls import path
from django.contrib import admin
from add_excel.views import IndexView
urlpatterns = [
url(r'index/', IndexView.as_view(), name ='index'),
]
urls.py in add_struc app:
from django.conf.urls import url
from django.urls import path
from django.contrib import admin
from add_struc.views import IndexView
urlpatterns = [
url(r'index/', IndexView.as_view(), name ='index'),
]
urls.py in homepage app:
from django.conf.urls import url
from django.urls import path
from django.contrib import admin
from homepage.views import index
urlpatterns = [
url(r'index/', index, name='index'),
]
views.py in add_excel app:
# -*- coding:utf-8 -*-
import os
import os.path
import sys
import datetime
from builtins import int
from django.db import models
from django.shortcuts import render
from django.views import View
import pymysql
import xlrd
from xlrd import xldate_as_tuple
# Create your views here.
class IndexView(View):
template_name = 'add_excel/index.html'
context={}
def get(self, request):
return render(request, 'add_excel/index.html', {})
def post(self, request):
# if request.method == 'POST' and 'excel_file' in request.POST:
if request.method == 'POST' and request.FILES["excel_file"]:
# do something
return render(request, 'add_excel/index.html', {"excel_data":excel_data})
You are using the name index in all of your url patterns which is causing a conflict. These should be properly namespaced (homepage-index, excel-index etc.) so when you refer to them throughout your application it is obvious to both developers and the program which URL should be chosen.
My theory is that there is some URL generation in your HTML Form that uses the name index and your Django site chooses the homepage for the URL so your IndexView is not run.
Sorry, I am still new at django. I want to make custom view at admin site that is not related to my model. I have read the documentation (https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls), but does not work. Reading some tutorials does not work too...
Here is what I tried:
admin.py
from django.contrib import admin
from django.urls import path
from .models import Question
from django.http import HttpResponse
class CustomAdminView(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path(r'^my_view/$', self.admin_site.admin_view(self.my_view))
]
urls = my_urls + urls
return urls
def my_view(self, request):
return HttpResponse("Hello, world.")
admin.site.register(Question)
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
admin.autodiscover()
urlpatterns = [
path(r'polls/',include('polls.urls')),
path('admin/', admin.site.urls),
]
when I go to admin/my_view the result is 404 not found.
I tried by extending the AdminView too.
admin.py
from django.contrib.admin import AdminSite
from django.urls import path
from .models import Question
from django.http import HttpResponse
class CustomAdminView(AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path(r'my_view/', self.admin_view(self.my_view))
]
urls = my_urls + urls
return urls
def my_view(self, request):
return HttpResponse("Hello, world.")
custom_admin = CustomAdminView()
custom_admin.register(Question)
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from polls.admin import custom_admin
admin.autodiscover()
urlpatterns = [
path(r'polls/',include('polls.urls')),
path('admin/', custom_admin.urls),
]
I don't get 404 error on admin/my_view. But, the default models(user, and others) are not displayed. There is only my 'Question' model there. The previous one still has the default models.
How can I make the custom admin view with the right way?
Thanks.
It is solved. I am using my second admin.py and urls.py snippets and register django's default model, based on this answer: Django (1.10) override AdminSite
admin.py
from django.contrib.admin import AdminSite
from django.http import HttpResponse
from django.urls import path
from .models import Question
from django.contrib.auth.models import Group, User #add these moduls
from django.contrib.auth.admin import GroupAdmin, UserAdmin #and these
class CustomAdminView(AdminSite):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path(r'my_view/', self.admin_view(self.my_view))
]
urls = my_urls + urls
return urls
def my_view(self, request):
return HttpResponse("Hello, world.")
custom_admin = CustomAdminView()
custom_admin.register(Question)
#register the default model
custom_admin.register(Group, GroupAdmin)
custom_admin.register(User, UserAdmin)
I'm just barely getting started with Python and Django.
I've created my modules, migrated, added my modules to the admin section, etc. Now, I need to create my first view. I've been following this guide (Yes the machine I'm on is running 1.6)
My views.py file in the app (setup) looks like this...
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Setup Index")
My app'a urls.py looks like this...
from django.conf.urls import patterns, url
from setup import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
And my root urls.py looks like this...
from django.views.generic import TemplateView
from django.views.generic.base import RedirectView
from django.conf.urls import include, patterns, url
from django.contrib import admin as djangoAdmin
from admin.controller import ReportWizard
from admin.forms import reportDetailsForm, reportDataViewsForm
from setup import views
djangoAdmin.autodiscover()
urlpatterns = patterns('',
url(r'^django-admin/', include(djangoAdmin.site.urls)),
#New User Wizard URL
url(r'^setup/', include('setup.urls')),
)
As far as I can tell I have followed the guide to the letter, but I am recieving a 404 error when navigating to myIP/setup.
There are no error messages on the 404 page. What is the issue?
For some reason your settings seem to override the default for either APPEND_SLASH or MIDDLEWARE_CLASSES.
Opening http://example.com/setup should cause a redirect to http://example.com/setup/, but somehow it doesn't for you.
Note that the latter URL is matched by your urls.py while the former is not (as should be the case).
The above should work if 'CommonMiddleWareis enabled andAPPEND_SLASHis set toTrue`.
I haven't found a satisfactory way of doing this: I have a djangocms setup that is working fine. But I need to add content from a table outside the CMS to my homepage and render that content on the template. I can do this, but editing the urls.py within CMS to use my views like so...
url(r'^', 'myapp.views.slideshow_info'),
... excludes any content from CMS. I understand that I just get my custom views to accommodate what CMS' views is doing, but how do I achieve this?
at the moment my app's views says:
from myapp.models import model1, model2
def slideshow_info(request):
return render_to_response('index.html', {'slideshow_list' : model1.objects.all()})
Many thanks
You can hook a custom app instance to any Django-CMS page. Here's the documentation on how to do so: http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks You shouldn't need to alter the base url patterns to specifically re-route / to your view.
Before custom app-hooks were available, I would accomplish what you're trying to do with template tags.
Hope that helps you out.
Followup
Ok, in a recently completed site, I had to hook an app titled "portfolio" to display images on the home page of a Django-CMS site.
Here are the relevant portions of the code:
#portfolio/cms_app.py
from django.utils.translation import ugettext_lazy as _
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class PortfolioAppHook(CMSApp):
name = _('Portfolio')
urls = ['portfolio.urls']
apphook_pool.register(PortfolioAppHook)
#portfolio/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('portfolio.views',
url(r'^(?P<slug>[-\w]+)/$', 'project_detail', name='project_detail'),
url(r'^$', 'portfolio_index', name='portfolio_index'),
)
#portfolio/views.py
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render
from portfolio.models import Project
def portfolio_index(request):
project_objects = Project.for_public if request.user.is_anonymous() \
else Project.objects
projects = project_objects.all().select_related(depth=1)
return render('portfolio/index.html',
{'projects' : projects}, request)
def project_detail(request, slug):
project = get_object_or_404(Project, slug=slug)
if not project.public and request.user.is_anonymous():
return HttpResponseRedirect('/?login=true')
return render('portfolio/project_detail.html',
{'project' : project}, request)
#urls.py (base urls)
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from views import login_user, logout_user
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/filebrowser/', include('filebrowser.urls')),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^login/$', login_user, name='login_user'),
url(r'^logout/$', logout_user, name='logout_user'),
(r'^', include('sorl.thumbnail.urls')),
(r'^', include('cms.urls')),
)
if settings.SERVE_STATIC_MEDIA:
urlpatterns += patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
As you can see from this working example, I haven't altered my base URLs to accommodate the home page view, rather I've provided the URLs for my Portfolio app to Django-CMS through cms_app.py
Hope that gets you going.