Related
This is the urls.py file for the main site in my Django project.
Ive got a category table and a country table linked to a product table and I cant get all the urls in the code below working. If i put the category_detail url in the list just after the slug:category_slug>/slug:slug/', product_detail url those urls work and country_detail links dont. If i put the country_detail urls after the slug:category-slug>/slug:slug/', product_detail url then only the country_detail url works. Any help appreciated
from django.contrib import admin
from django.urls import path
from core.views import frontpage
from store.views import product_detail, category_detail,country_detail
urlpatterns = [
path('', frontpage, name='frontpage'),
path('admin/', admin.site.urls),
path('<slug:category_slug>/<slug:slug>/', product_detail, name='product_detail'),
path('<slug:slug>/', country_detail, name='country_detail'),
path('<slug:slug>/', category_detail, name='category_detail'),```
If i put the category_detail url in the list just after the slug:category_slug>/slug:slug/', product_detail url those urls work and country_detail links dont. If i put the country_detail urls after the slug:category-slug>/slug:slug/', product_detail url then only the country_detail url works.
This is because Django will use the first route that matches. Since the rules to match country detail and product detail are both exactly the same, whichever one is first is the one that will match.
To solve this, you need to rethink your API design and create routes that can be matched unambiguously.
Often we design a REST API around "resources" and "collections". This means we will have a route /products/ such as to interact with a collection and /products/1 to interact with a specific product resource.
For the collection route /products, we can add the ability to pass in query parameters to filter on a category or country with something like /products?country=United+States (note the + is the URL encoded version of a space). To allow this on the API side, you will need to get the query parameters from the request object and then add a filter to your queryset. I won't get into the details here, but you can find more information about this with some googling.
For the resource route products/1, we might want to get related objects, like the categories for that country. In such a case, we would create a route like products/1/categories. I will leave the details for implementing the view for this as an exercise for the reader.
Try this
urlpatterns = [
path('', frontpage, name='frontpage'),
path('admin/', admin.site.urls),
path('product-detail/<slug:category_slug>/<slug:slug>/', product_detail, name='product_detail'),
path('country-detail/<slug:slug>/', country_detail, name='country_detail'),
path('category-detail/<slug:slug>/', category_detail, name='category_detail'),
]
I'm following a django tutorial that is a little outdated and in the urls.py file of the first app's directory we need to configure where to direct django to for any url starting with 'notes/'.
There are two separate 'apps' inside the project. I'm in the first one, not notes.
This is the code currently. I added include to import statement:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(url(r’^notes/‘, include('notes.urls'))),
]
Inside the urlpatterns object, on the first line, path('admin/', admin.site.urls), comes predefined, but I need to add a redirect such that django goes to a different app, called 'notes' and searches there for its entry point, since all of the urls will begin with ‘notes/’.
The tutorial says to use a regular expression and uses this code:
url(r’^notes/‘, include(notes.urls))
so that any url that starts with 'notes/' should be redirected to this other file notes.urls.
However the predefined ones that currently come out of the box with a django project start with path.
I enclosed my notes/n redirect line in path, but not sure if this is correct. Should I instead directly write:
url(r’^notes/‘, include(notes.urls))
Also, do I need to delete the first line provided?
path('admin/', admin.site.urls),
The tutorial has:
urlpatterns = patterns('',
url(r’^notes/‘, include(notes.urls)),
)
and no admin urls line. It's from 2014 I believe.
Simply do:
path('notes/', include('notes.urls'))
I have an app called 'vocab', which so far contains a CreateView and a ListView. In my project urls.py file, I have the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.HomePage.as_view(),name='home'),
re_path(r"^accounts/", include("accounts.urls", namespace="accounts")),
re_path(r"^accounts/", include("django.contrib.auth.urls")),
re_path(r"^logout/$", views.LogOutPage.as_view(), name="logout"),
re_path(r"^vocab/", include("vocab.urls", namespace="vocab")),
]
And then, in my app urls.py file, I have the following:
urlpatterns = [
path('list/', views.WordList.as_view(), name="list"),
path("home/",views.VocabHome.as_view(),name='home'),
path("create/", views.CreateWord.as_view(), name="create"),
]
And in my html templates I have links to these views. The home and list views work, but when I click on the 'create' link, it takes me to 'list' instead. In the command line I can see that it is accessing /vocab/list/? instead of /vocab/create/. I know the create view works, because if I type it in manually it goes there. And if I delete all the other views it works as well. So it is as if django cannot find the 'create' view when there are others. I've tried my best to solve this problem by making sure all the paths are unique, and I've tried using regular expressions, but it still doesn't work. Any help would greatly be appreciated!
I found the mistake...
I had a missing form closing tag in my html. This resulted in my second form (and therefore link) being invalid.
env:
Django==1.7.8
wagtail==0.8.8
execute "manage.py runserver" to launch the website
I copy a example.mp4 file to /static dir, then can open it in browser#127.0.0.1:8000/statice/example.mp4
but when I insert http://127.0.0.1:8000/statice/example.mp4 to page as a embed component, the server return a 500 error.
then I use another internet url, http://viemo.com/86036070, and wagtail still show a 500 error.
the embed url will be send to /admin/embeds/chooser/upload/
I decide to review the related source code to find out, but...
top urls.py:
urlpatterns = patterns('',
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^search/', include(wagtailsearch_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url('^sitemap\.xml$', sitemap),
url(r'', include(wagtail_urls)),
)
and admin_usrls:
from django.conf.urls import url
from wagtail.wagtailadmin.forms import PasswordResetForm
from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar, page_privacy
from wagtail.wagtailcore import hooks
urlpatterns += [
url(r'^$', home.home, name='wagtailadmin_home'),
url(r'^failwhale/$', home.error_test, name='wagtailadmin_error_test'),
url(r'^explorer-nav/$', pages.explorer_nav, name='wagtailadmin_explorer_nav'),
url(r'^pages/$', pages.index, name='wagtailadmin_explore_root'),
url(r'^pages/(\d+)/$', pages.index, name='wagtailadmin_explore'),
url(r'^pages/new/(\w+)/(\w+)/(\d+)/$', pages.create, name='wagtailadmin_pages_create'),
url(r'^pages/new/(\w+)/(\w+)/(\d+)/preview/$', pages.preview_on_create, name='wagtailadmin_pages_preview_on_create'),
url(r'^pages/usage/(\w+)/(\w+)/$', pages.content_type_use, name='wagtailadmin_pages_type_use'),
url(r'^pages/(\d+)/edit/$', pages.edit, name='wagtailadmin_pages_edit'),
url(r'^pages/(\d+)/edit/preview/$', pages.preview_on_edit, name='wagtailadmin_pages_preview_on_edit'),
url(r'^pages/preview/$', pages.preview, name='wagtailadmin_pages_preview'),
url(r'^pages/preview_loading/$', pages.preview_loading, name='wagtailadmin_pages_preview_loading'),
url(r'^pages/(\d+)/view_draft/$', pages.view_draft, name='wagtailadmin_pages_view_draft'),
url(r'^pages/(\d+)/add_subpage/$', pages.add_subpage, name='wagtailadmin_pages_add_subpage'),
url(r'^pages/(\d+)/delete/$', pages.delete, name='wagtailadmin_pages_delete'),
url(r'^pages/(\d+)/unpublish/$', pages.unpublish, name='wagtailadmin_pages_unpublish'),
url(r'^pages/search/$', pages.search, name='wagtailadmin_pages_search'),
url(r'^pages/(\d+)/move/$', pages.move_choose_destination, name='wagtailadmin_pages_move'),
url(r'^pages/(\d+)/move/(\d+)/$', pages.move_choose_destination, name='wagtailadmin_pages_move_choose_destination'),
url(r'^pages/(\d+)/move/(\d+)/confirm/$', pages.move_confirm, name='wagtailadmin_pages_move_confirm'),
url(r'^pages/(\d+)/set_position/$', pages.set_page_position, name='wagtailadmin_pages_set_page_position'),
url(r'^pages/(\d+)/copy/$', pages.copy, name='wagtailadmin_pages_copy'),
url(r'^pages/moderation/(\d+)/approve/$', pages.approve_moderation, name='wagtailadmin_pages_approve_moderation'),
url(r'^pages/moderation/(\d+)/reject/$', pages.reject_moderation, name='wagtailadmin_pages_reject_moderation'),
url(r'^pages/moderation/(\d+)/preview/$', pages.preview_for_moderation, name='wagtailadmin_pages_preview_for_moderation'),
url(r'^pages/(\d+)/privacy/$', page_privacy.set_privacy, name='wagtailadmin_pages_set_privacy'),
url(r'^pages/(\d+)/lock/$', pages.lock, name='wagtailadmin_pages_lock'),
url(r'^pages/(\d+)/unlock/$', pages.unlock, name='wagtailadmin_pages_unlock'),
url(r'^choose-page/$', chooser.browse, name='wagtailadmin_choose_page'),
url(r'^choose-page/(\d+)/$', chooser.browse, name='wagtailadmin_choose_page_child'),
url(r'^choose-external-link/$', chooser.external_link, name='wagtailadmin_choose_page_external_link'),
url(r'^choose-email-link/$', chooser.email_link, name='wagtailadmin_choose_page_email_link'),
url(r'^tag-autocomplete/$', tags.autocomplete, name='wagtailadmin_tag_autocomplete'),
# 登陆
url(r'^login/$', account.login, name='wagtailadmin_login'),
url(r'^account/$', account.account, name='wagtailadmin_account'),
url(r'^account/change_password/$', account.change_password, name='wagtailadmin_account_change_password'),
url(r'^account/notification_preferences/$', account.notification_preferences, name='wagtailadmin_account_notification_preferences'),
url(r'^logout/$', account.logout, name='wagtailadmin_logout'),
url(r'^userbar/(\d+)/$', userbar.for_frontend, name='wagtailadmin_userbar_frontend'),
url(r'^userbar/moderation/(\d+)/$', userbar.for_moderation, name='wagtailadmin_userbar_moderation'),
]
# This is here to make sure that 'django.contrib.auth.views.login' is reversed correctly
# It must be placed after 'wagtailadmin_login' to prevent this from being used
urlpatterns += [
url(r'^login/$', 'django.contrib.auth.views.login'),
]
# Import additional urlpatterns from any apps that define a register_admin_urls hook
for fn in hooks.get_hooks('register_admin_urls'):
urls = fn()
if urls:
urlpatterns += urls
I don't find which view /admin/embeds/chooser/upload/ will be routed to. but I am sure there is a view function map to this url, because I add a "print" in django.forms.forms.is_valid method, and it be triggered.
can anybody help me? thanks in advance, and sorry for my D-level english.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update:
I create a new wagtail project, and write a simple page model, then I insert a embed to the content field, a RichTextField, the server return OK-200, but nothing was insert to content in edit panel.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
update:
the view for the path is wagtailembeds.views.chooser.chooser_upload. I will check it.
thanks all.
I'm trying to get any sort of nested inline to work with python 3 and django 1.8.
I tried django super-inlines and that didn't really make them nested, they just kinda showed up next to eachother.
I'm currently trying django-nested-admin and I get a page not found error with this pattern search:
^ ^$ [name='browse']
^admin/ ^server-data.js$ [name='nesting_server_data']
browse links to a separate set of urls and this just will not load any admin links.
urls.py:
from django.conf.urls import include, url
import nested_admin
urlpatterns = [
url(r'^', include('flightdb.urls')),
url(r'^admin/', include('nested_admin.urls')),
]
Connecting to /admin/ gets a 404 not found error.
Any help with why the page isn't loading or any better way to get nested-inlines to work would be greatly appreciated.
The pattern r'^' will match anything that also matches r'^admin/'. Django performs the URL matching by starting from the beginning of urlpatterns and progresses until it finds the first match. So in this case things that were supposed to be in admin are matching '^' which is flightdb.urls where they won't be found. You could potentially fix this issue by reordering the url's as follows:
urlpatterns = [
url(r'^admin/', include('nested_admin.urls')),
url(r'^', include('flightdb.urls')),
]
Now admin is checked for a match before the rest of the top level urls.
EDIT: I suspect your issues are coming from using "admin" in your url pattern to go to somewhere other than the default Django admin package. I did some searching around in the source of django-nested-admin and in particular came across something here:
https://github.com/theatlantic/django-nested-admin/blob/master/nested_admin/options.py in particular have a look at line 275:
context = {
'title': _('Add %s') % force_unicode(opts.verbose_name),
'adminform': adminForm,
'is_popup': (IS_POPUP_VAR in request.POST or
IS_POPUP_VAR in request.GET),
'show_delete': False,
'media': mark_safe(media),
'inline_admin_formsets': inline_admin_formsets,
'errors': helpers.AdminErrorList(form, formsets),
'root_path': reverse('admin:index'),
'app_label': opts.app_label,
}
Here there is a call to reverse('admin:index'). This needs to resolve to the usual admin path in order to work. When you have your url's defined like you do you run into the problem of admin now being something other than the default that django-nested-admin is expecting to see.
Perhaps try a different url pattern that is not "admin" such as the one suggested by their documentation:
url(r'^nested_admin/', include('nested_admin.urls')),
This way the reverse used in the internals of the django-nested-admin package will work.
I had the same problem. This is what my settings.py and urls.py look like
settings.py
INSTALLED_APPS = [
'nested_admin',
'django.contrib.admin',
# ...
]
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^nested_admin/', include('nested_admin.urls')),
#...
]
Now, don't go to yourproject.com/nested_admin but yourproject.com/admin .
You still need the include for nested_admin or it won't work.