Whenever I try to log in with username and password this happens:
my project name is:corey_schafer
app name for user stuff is: users
projects urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('register/', include('users.urls'))
]
users urls.py :
from django.urls import path
from . import views
urlpatterns = [
path('', views.register, name='register'),
path('signup', views.signup, name='signup'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
]
users views.py:
[![enter image description here][2]][2]
if views.py image not showing please go here:https://i.stack.imgur.com/auZxo.png
if any other file is needed to solve this problem please let me know
You url basically needs to have only register/login/ path rather than two /logins.
Your path defined is first go to register.urls and inside that go match and go to login as per the URLs files mentioned above. Whereas the path in the question that you are trying to access says go to register then login and login again which isn't available, thus 404!
You need to add app_name parameter in users urls.py in order to be able to have redirect automatically use reverse to correct view under the hood.
Related
So hello guys i have run into trouble while running my project.
this is my urls.py
from unicodedata import name
from django.urls import path
from django.contrib import admin
from django.urls import re_path
from . import views
app_name = "Project"
urlpatterns = [
path('', views.index , name='index'),
path('counter', views.counter, name='counter'),
path('Register', views.Register, name= 'Register'),
path('login', views.login, name='login'),
path('logout', views.logout, name = 'logout'),
path('post/<str:pk>', views.post, name = 'post'),
path('profile', views.profile, name='profile'),
re_path(r'^appointment/appointment=(?P<appointment_id>[0-100]+)', views.viewAppointment,
name='appointment'),
re_path(r'^appointment/appointment=(?P<appointment_id>[0-100]+)/AddDiagnonsis',
views.addDiagnosis, name='AddDiagnosis')
]
and this my views.py
def viewAppointment(request, appointment_id):
appointment = Appointment.objects.filter(id=appointment_id)
return render(request, 'appointment_form.html', {'Appointment': appointment})
i dont know what do im new here at django
You don't have a URL referenced as appointment, only appointment/appointment=... and appointment/appointment=.../AddDiagnonsis (which you probably misspelled btw)
If you tried going to this url by writing it, you wrote it wrong or forgot to put an url appointment in your urlpatterns.
I don't know why this problem happened.enter image description here
These errors occur when you read a book and follow it. I don't know what to do. Give me a hand.
Source Code
This is my fistsite.py source code.
fistsite.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
enter code here
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
After changing the source code above
now fistsite.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', views.polls, name='polls'),
path('/admin', views.admin, name='admin')
]
But there was an error.enter image description here
my projcetenter image description here
Assuming that you have Django version > 2.0, you should add to your urls.py file something like this:
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', views.polls, name='polls'),
path('/admin', views.admin, name='admin')
]
So as shown in the error page, your Django project tried to go to localhost:8000/admin/ and localhost:8000/polls/ but the site you are trying to access in localhost:8000/ . You haven't defined any url pattern for capturing this scenario. What you can do is in your urls.py, add this line under urlpatterns :
urlpatterns=[
path('', views.index, name='index'),
...............
]
This works if you are using Django version>2.0
If you are using Django version<2.0 then you can add this:
urlpatterns=[
url(r'^$', views.index, name='index'),
...............
]
If you want to access you "polls". After change your fistsite.pytry this
url = "localhost:8000/polls/
and you are giving only localhost:8000 if did not create any app then this url will work.
Im trying to add this url to my app's urlpatterns (i.e. MyProject/MyApp/urls.py):
url(r'^login/$', auth_views.LoginView.as_view(), name='login')
I have this snippet in one of my templates:
Login
Normally, clicking on the link takes you to the login page successfully. However, when I try to add a namespace to my urls (app_name = my_namespace) and change the reverse to
Login
it fails when I click on the link and I get the error
Reverse for 'login' not found. 'login' is not a valid view function or
pattern name.
While all the other urls I reverse work with the namespace, it is just the login reverse that fails. Any idea why?
Edit:
MyProject/MyProject/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('ClubInfo.urls')),
]
MyProject/MyApp/urls.py:
app_name = 'clubinfo'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', auth_views.LoginView.as_view(), name='login'),
]
A snippet of the template:
Home
Login
Register
I can click on Home and Register, not login
Edit 2: auth_views is from this import:
from django.contrib.auth import views as auth_views
I think this may have something to do with why the program is raising an error.
It turns out the problem was in my login.html file which Django renders in its LoginView. I didn't use the namespace in one of my reverses in that file.
in your project urls do this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('ClubInfo.urls', namespace='clubinfo')),
]
now in clubinfo urls:
remove
app_name = 'clubinfo'
run de server again and try it should work that my way of doing
I find that your
app_name='clubinfo'
, but your urlpatterns is
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('**ClubInfo**.urls')),
]
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
I followed http://blog.narenarya.in/right-way-django-authentication.html in order to add a user authentication in my Django project but when I migrate an error occured in my project urls.py but I didn't find it !!!
newsite/newsite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from aps import views
from mail import views
from log import views
from django.contrib.auth import views
from log.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^aps/', include('aps.urls')),
url(r'^mail/', include('mail.urls')),
url(r'^log/', include('log.urls')),
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm},
url(r'^logout/$', views.logout, {'next_page': '/login'}),
]
You're missing the last ).
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}),
I highly suggest you go back to the link that you provided as your source. You've miscopied some of the code. For example this line from the example provided:
url(r'^admin/', include(admin.site.urls)),
You did not provide the error message/trace you received, so of the couple of problems it could be, its impossible to say which one is giving you the error you are currently receiving -- but again, compare your code to the example for errors (including in indentation). If it still doesn't work, provide the error message you receive