I don't understand why django wont render any other template , but home with this code:
urlpatterns = [
url(r'^login/', login_page),
url(r'^admin/', admin.site.urls),
url(r'^contact/', contact_page),
url(r'^about/', about_page),
url(r'$', home_page),
]
But as soon as I delete the home url , every url works just fine. Yes , I did join the base dir with the templates one. Also , it might be useful to add that Im using django 1.11. Thank you !
Related
Here is the problem:
I have an app with the following models: project, position, outreach
A position is connected to a project and project only with a Foreign key
An outreach is connected to a position and a position only with a Foreign key
I can create a new project from almost anywhere in my app (same for the other objects). Currently I wrote that a new project is created from the url dashboard/newjobproject but I would to make it so that depending on the page that I am, the url simply becomes something like www.myapp.com/..../newproject
What's a way to write the urls.py to achieve that?
from django.urls import path
from action import views
app_name = 'action'
urlpatterns = [
# ex: /action/
path('', views.login, name='login'),
path('dashboard/', views.dashboard, name='dashboard'),
path('contacts/', views.contacts, name='contacts'),
path('projects/', views.project, name='project'),
path('contacts/newcontact', views.new_contact, name='new_contact'),
path('projects/newjobproject', views.new_outreach, name='new_outreach'),
path('dashboard/newjobproject', views.new_jobproject, name='new_jobproject'),
path('projects/<uuid>/newjobposition', views.new_jobposition, name='new_jobposition'),
]
However,
Try adding this to the bottom of urlpatterns:
path('<path:p>/newjobproject', views.new_jobproject, name='whatever-name-you-want'),
and in your views.py:
def new_jobproject(request, p):
Tbh though, this is sort of a hacky way to do it. It'll break in a few locations. If you have a main urlpatterns array in which you're including the urls for this 'action' app as APIs, this solution won't work outside the API urls.
For eg. if you have:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('action.urls')),
]
And you access your url like this -> www.myapp.com/api/v1/....../newjobproject, then the code will work. If you try to access www.myapp.com/..../newjobproject or www.myapp.com/admin/..../newjobproject then the code will break and it will not match any of the paths. I'm not sure how you'd get it to work in that case.
If the above scenario is not an issue, if you're not going to be using these views as APIs, and your urlpatterns looks something like this:
urlpatterns = [
path('admin', admin.site.urls),
path('/', include('action.urls')),
]
then the code should work for all urls except for the admin/.../newjobproject case.
I'm trying to send a custom email template with the reset token. But it keeps sending raw HTML no matter what I do. I have previous experience of doing exactly the same thing without any problem with the Django and Django rest framework.
Here's how I did it,
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls')),
path('', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
store/urls.py
...
path('password_reset',
auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name = 'registration/password_reset_email.html',
),
name='password_reset'
),
folder structure
...
templates
|-- registration
|-- html_password_reset_email.html
|-- password_reset_email.html
|-- ...
...
Note:
I tried deleting the password_reset_email.html template after that it sends the default Django email template.
Hoping for any guidance to solve the issue thanks in advance.
Finally days(3) of debugging I found my dumb mistake :(,
I was including all auth urls path('', include('django.contrib.auth.urls')) in my main urls.py file and re defining the auth.urls again in store/urls.py differently so the overriding doesn't happen correctly.
So, instead of,
path('password_reset', # <<< here
auth_views.PasswordResetView.as_view(
...
),
name='password_reset'
),
it should be,
path('password_reset/', # <<< here
auth_views.PasswordResetView.as_view(
),
name='password_reset'
),
Not gonna delete the question (^.^') #for_sake_of_dumb_fellows_like_me
I have a urls :
urlpatterns = [
url(r"admin/" , admin.site.urls),
url(r"users/" , include(("users.urls", "users") , namespace='users') ),
path('bristol/' , include(('bristol.urls', "bristol") , namespace='bristol') ),
url(r"" , include('django.contrib.auth.urls')),
]
But I can't connect to my server if I don't put any end slug to my url :
What should I change in my url patterns to redirect localhost:8000 directly to the login view ?
I tried :
urlpatterns += [ url(r"/" , reverse("login"))]
But It didn't help :-/
Edit 1 :
Trying Wilem's solution worked at last, and the issue was partially due to refresh issues on my browser (see comments):
Since django.contrib.auth.urls does not contain any view with path /, it will thus not match with any pattern.
You can define an extra pattern that for example maps on the LoginView:
from django.contrib.auth.views import LoginView
urlpatterns = [
url(r'admin/', admin.site.urls),
url(r'users/', include(('users.urls', 'users'), namespace='users')),
path('bristol/', include(('bristol.urls', 'bristol'), namespace='bristol')),
url(r'', include('django.contrib.auth.urls')),
path('', LoginView.as_view(), name='index')
]
But it is rather odd to map the index path on the LoginView, normally you make a view that acts as some sort of "dashboard". You can make use of the #login_required decorator [Django-doc] to prevent people from looking at the dashboard if they are not logged in.
some question about django 1.10:
I am going to make a website and the home page should be a login page created by django,when i tried to make the admin login page as my site's home page,it failed,can anybody help me ?
my urls.py:
urlpatterns = [ url(r'^$', include(admin.site.urls)), ]
or
urlpatterns = [ url(r'^$', admin.site.urls), ]
both works wrong.
thx
For djanog version 2 & later, the syntax url() has been replaced & simplified with path()
So, if you have django version >=2 , use this:
urlpatterns = [
path('', admin.site.urls),
]
Read the docs for details.
Generally speaking, setting admin page as the main page is not a good idea but still, whatever the use case is, try this:
url(r'^', admin.site.urls),
Tested it on Django 1.9 but I think it'll work on Django 1.10 as well.
Also, don't forget to restart the server after making these changes.
I recently upgraded my Django project to version 1.9.
When I try to run migrate, I am getting the following two errors:
Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got app.views.about). Pass the callable instead.
django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
Could someone please show me the proper syntax of how to do this? A brief sample of my urls.py is below:
urlpatterns = patterns('',
url(r'^about/$', 'app.views.about',
name='about'),
)
urlpatterns += patterns('accounts.views',
url(r'^signin/$', 'auth_login',
name='login'),
)
Thank you!
Import your views directly, or your views modules:
from apps.views import about
from accounts import views as account_views
Do not use patterns at all, just use a list or tuple:
urlpatterns = [
url(r'^about/$', about,
name='about'),
]
urlpatterns += [
url(r'^signin/$', account_views.auth_login,
name='login'),
]
You should remove the quotes around views name.
So your code will be like that
urlpatterns = patterns('',
url(r'^about/$', app.views.about, #without quote!
name='about'),
)
Point 2, use lists, so your code will transform to
urlpatterns = [
url(r'^about/$', app.views.about, #without quote!
name='about'),
]