How to set a html page in Django? - python

I started to follow this tutorial and it teaches how to load the index.html, but now I need to develop a login.html page, but it seems not be working properly.
urls.py
urlpatterns = patterns('',
url(r'^$', 'provisioning.views.home', name='home'), <- it works!
url(r'^$', 'provisioning.views.login', name='login'), <- doesn't work..
url(r'^admin/', include(admin.site.urls)),
)
views.py
def login(request):
return render(request, 'login.html')
How to setup the pages to be loaded by django ?
Is there another and better way in doing this ?

You have the same URLs for both home and login. The regex pattern r'^$' specifies that nothing comes after your local host. Because they are the same and Django checks URLs sequentially, only the first url and view is called. Try adding a different url for login.
url(r'^login/$', 'provisioning.views.login', name='login')

Related

How to place links in django templates

VERY new to Django and I am a little confused. I have placed a link in my html template file; however, clicking the link reports a 404 error.
App views.py file:
from django.shortcuts import render
def index(request):
return render(request, "login/index.html", None)
def terms(request):
return render(request, "login/terms.html", None)
App urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
path('', views.terms, name="terms")
]
Offending code in index.html:
By signing in, you agree to our Terms Of Use Policy
Clicking on the link pops a 404 error. Any help would be appreciated as I learn a new framework.
The first problem is that both the path to the views.index and views.terms share the same path. As a result, you have made views.terms inaccessible.
You thus should change one of the paths, for example:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('terms/', views.terms, name='terms')
]
You better use the {% url ... %} template tag [Django-doc] to resolve the URLs, since if you later decide to change the path of a certain view, you can still calculate the path.
In your template, you thus write:
By signing in, you agree to our Terms Of Use Policy
urlpatterns = [
path('', views.index, name="index"),
path('terms/', views.terms, name="terms")
]
By signing in, you agree to our Terms Of Use Policy
or

Django redirecting everything to homepage

I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page
For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.
Here is my code:
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('portal.urls')),
url(r'^login/', include('login.urls')),
]
login urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/', views.index, name="login"),
]
login views.py
from django.shortcuts import render
def index(request):
return render(request, 'login/login.html')
portal urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name="portal"),
]
I see 2 problems here:
As #DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.
You don't terminate the portal index URL, so it matches everything. It should be:
url(r'^$', views.index, name="portal"),
In addition, if the regex is login/$ but you enter http ://server/login, then it won't match wheras http://server/login/ will.
You could try changing the regex to login/*$, which will match any number (even zero) / on the end of the url.
So http: //server/login, http: //server/login/, http: //server/login//// would all match.
Or if you want to be specific, login/{0,1}$ might work (though that regex syntax is from memory!)

django-cms and admin interface confilct

I have created custom index view. urls.py:
url(r'^', include('cms.urls')),
url(r'^', 'myapp.views.index', name='index'),
in views.py:
from cms.utils import get_template_from_request
def index(request):
template = get_template_from_request(request)
.....
return render(request, template)
When i try to access django admin 127.0.0.1:8000/admin i get an error
'NoneType' object has no attribute 'pk'
because in my index.html is templatetag {% product_list request.current_page %} which requires current_page to be in request. I think this happens because django renders my index page in django admin, where it shouldn't. What can i do to fix this?
I think the easiest way to fix your problem is to include the urls of the admin site before those 'index' and 'cms' as explained in the Django documentation site. Your url patterns in the urls.py file would be something like this:
...
url(r'^admin/', admin.site.urls),
url(r'^', include('cms.urls')),
url(r'^', 'myapp.views.index', name='index'),
...
Previously i had url(r'^myapp/', include('myapp.urls')), changed to url(r'^', include('myapp.urls')) and it's working.

django-registration-redux change password link

I am using django-registrations-redux. In my template, I have the following:
html:
<li>Change Password</li>
and the urls.py is:
url(r'^accounts/', include('registration.backends.default.urls')),
if I go to the following
http://127.0.0.1:8000/accounts/
I get the list of all the links that works under accounts/ such as login, logout and register...etc
If i go to accounts/login it works really fine but if i go to /accounts/password/change in the case of the html provided above, the page redirects to an admin look and feel page witha Django Administration banner on the top of the page.
any help? am i missing anything?
edit:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/', "asset.views.home", name="home"),
url(r'^add_asset/', "asset.views.add_asset", name="add_asset"),
url(r'^update_asset/(?P<id>\d+)/$', "asset.views.update_asset", name="update_asset"),
url(r'^asset_detail/(?P<slug_asset_desc>[\w\-]+)/$', "asset.views.asset_detail", name='asset_detail'),
url(r'^search/$', "asset.views.search_asset", name="search"),
url(r'^accounts/', include('registration.backends.default.urls')),]
The full url for password changing is "password/change/" with slash at the end. Maybe in your settings you set APPEND_SLASH to false and it's can't find "password/change" url. Try to use pattern name in your html code:
<li>Change Password</li>

wagtail return server error 500 when insert embed

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.

Categories