SyntaxError:InvalidSyntax? - python

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

Related

Django : Page not found (404)

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.

I have an error in my Django accounts app

I was following the tutorial on https://simpleisbetterthancomplex.com/series/2017/09/25/a-complete-beginners-guide-to-django-part-4.html and I have an error message which says that account_views is not defined. In my urls.py file which is located in C:\Users\vitorfs\Development\myproject\myproject\myproject. Here is the code
from django.conf.urls import url
from django.contrib import admin
from accounts import views as account_views
from boards import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^signup/$', accounts_views.signup, name='signup'),
url(r'^boards/(?P<pk>\d+)/$', views.board_topics, name='board_topics'),
url(r'^boards/(?P<pk>\d+)/new/$', views.new_topic, name='new_topic'),
url(r'^admin/', admin.site.urls),
]
The error is on line 25, url(r'^signup/$', accounts_views.signup, name='signup'), which states that account_views is not defined. I would like to know why I am getting this error, and also, can you show me how to fix this error?
When you are importing the accounts in the top of the file, you are saying from accounts import views as account_views. You are importing it as account_views so in your urls, you should say account_views not accounts_views.

How to change the default Django page to my own one?

I'm learning Django,and I followed a guidebook.I want to change the default Django page into my own one.It has took me 2 hours to solve this,and nothing worked.
The project is called learning_log,and the app is called learning_logs.
Here's what I'm trying to do:
1.add module learning_logs.urls:
"""
Definition of urls for learning_log.
"""
from datetime import datetime
from django.urls import path
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView
from app import forms, views
#added
from django.conf.urls import include, url
import learning_logs.views
from django.urls import path,re_path
app_name='learning_logs'
urlpatterns =[
path('', views.home, name='home'),
path('contact/', views.contact, name='contact'),
path('about/', views.about, name='about'),
path('login/',
LoginView.as_view
(
template_name='app/login.html',
authentication_form=forms.BootstrapAuthenticationForm,
extra_context=
{
'title': 'Log in',
'year' : datetime.now().year,
}
),
name='login'),
path('logout/', LogoutView.as_view(next_page='/'), name='logout'),
path('admin/', admin.site.urls),
#added
re_path(r'',include('learning_logs.urls',namespace='learning_logs'))
]
To include the module,I've tried:
url(r'',include('learning_logs.urls',namespacec='learning_logs'))
path('',include('learning_logs.urls',namespacec='learning_logs'))
path('',include('learning_logs.urls'))
path('',learning_logs.urls)
path('',learning_logs.views)
But none of them worked.
2.Create urls.py in learning_logs.here's the code:
"""define learning_logs's url mode"""
from django.urls import path,re_path
from . import views
from django.conf.urls import include,url
urlpatterns=[
#homepageļ¼š
re_path(r'(?P^$)',views.index,name='index')]
#path('',include('learning_logs.views.index'))]
As you can see,I also tried many times.
3.Write views.py in learning_logs
from django.shortcuts import render
# Create your views here.
def index(request):
"""homepage of learning logs"""
return render(request,'learning_logs/index.html')
4.write HTML document in learning_logs/templates/learning_logs/index.html
code skipped,for it never came up.
Here is my document tree:
tree
What I want is that show my own homepage at localhost:xxxxx/
How to solve this?
it's simple. just change this
urlpatterns =[
path('', views.home, name='home'),
to this
urlpatterns =[
path('', learning_logs.views.index, name='home'), #correct path to your index view
I don't understand what you're trying to do, why don't you just use:
urlpatterns =[
path('', views.home, name='home'),
And change the name so it directs you to that home page instead?
E.g.
urlpatterns =[
path('', views.learning_logs, name='learning logs'),
Also, since learning logs IS in the views file you shouldn't use
path('',learning_logs.views)
Does that make sense?

Why am I getting a 404 error when trying to access my website's domain name, yet if I domain name/home it works?

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')),
]

does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import

I got
does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import error .
I wrote in urls.py of child app
from django.conf.urls import url
from django.contrib.views import login,logout
urlpatterns = [
url(r'^login/$', login,
name='login'),
url(r'^logout/$', logout, name='logout')
]
in urls.py of parent app,
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
url(r'^api/', include('UserToken.urls')),
url(r'^UserDataAPI/', include('UserDataAPI.urls', namespace='UserDataAPI')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am thinking urls.py of child or parent app,but I do not know how to fix it.
What should I do?
Your import is incorrect. The views.py containing login and logout is in the django.contrib.auth app:
from django.contrib.auth.views import login, logout
It is caused by errors in views.py file as code code redirect link isn't properly working.
so,Check views file to remove error
just check whether urlpattern and change into patterns
In your outermost urls file, if you are including other package urls like
path("accounts/", include("UserDataAPI.urls"))
or
url(r'^accounts/', include('accounts.urls'))
Ensure that in accounts.urls module or UserDataAPI.urls modules you have declared the urlpatterns variable

Categories