Syntax error in urls.py - python

I was trying to add an editing/deleting function for a bookmarks app.
I get the following error:
My Urls.py file is the following:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'bookmarks.views.index', name='home'),
url(r'^bookmarks/$', 'bookmarks.views.index', name='bookmarks_view'),
url(r'^tags/([\w-]+)/$', 'bookmarks.views.tag'),
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})
url(r'^delete/(\d+)/$', 'bookmarks.views.delete'),
url(r'^edit/(\d+)/$', 'bookmarks.views.edit'),
)
The views.py file:
...remaining code
def delete(request, bookmark_id):
if request.method == 'POST':
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
b.delete()
return redirect(index)
def edit(request, bookmark_id):
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
context = {
'form' : BookmarkForm(instance=b),
}
return render(request, 'edit.html', context)
The bookmark widget in index.html
{% block bookmark_widget %}
{% if request.user %}
<div id="new-bookmark-widget">
<form method="post" action="{% url bookmarks.views.index %}">
{% csrf_token %}
<h3>Bookmark</h3>
{{ form.as_p }}
<p><button id="new-bookmark-submit">Submit</button>Submit</button>
</form>
</div>
{% endif %}
{% endblock %}
The relevant div in base.html
<div id="container">
<div id="header">
{% block bookmark_widget %}
{% endblock %}
<div id="authentication">
{% if user.is_authenticated %}
Hi {{user}}! Logout
{% else %}
Login
{% endif %}
</div>
<h1>My bookmarking app</h1>
</div>
<div id="content">
<h2>{% block subheader %}{% endblock %}</h2>
{% block content %}
Sample content -- you should never see this, unless an inheriting template fails to have any content block!
{% endblock %}
</div>
<div id="footer">
All copyrights reserved
</div>
</div>
The complete bookmark.html file:
<li>
<a class="bookmark-link" href="{{ bookmark.url }}">{% if bookmark.title %}{{ bookmark.title }}{% else %}{{ bookmark.url }}{% endif %}</a>
<div class="metadata"><span class="author">Posted by {{ bookmark.author }}</span> | <span class="timestamp">{{ bookmark.timestamp|date:"Y-m-d" }}</span>
{% if bookmark.tag_set.all %}| <span class="tags">
{% for tag in bookmark.tag_set.all %}
{{ tag.slug }}</span>
{% endfor %}
{% endif %}
</div>
{% if request.user.is_authenticated %}
<div class="actions"><form method=="POST" action="{% url bookmarks.view.delete bookmark.id %}>
{% csrf_token %}
<input type="submit" value="Delete">
</form> </div>
{% endif %}
I am new to Django, how do I handle error mistakes and how do I find the problem? I couldnt see it from the error message.
Thank you!

missing a comma on the following line:
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}), <-add here
I presume your comments section also lines up with the rest of your code and it is formatted incorrectly in your question

You're missing a comma at the end of this line:
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'})

Related

PasswordResetConfirmView..as_view() in Django redirect to Django administration site

I am using Django 3 to develop a website where password reset has done through e-mail conformation.
After clicking the link in the email, it takes to the Django Administration Password reset confirmation page, rather taking me to the my developed web page. I am following Django 3 by example book. These codes is from chapter 4 of this book.
Please help. Thanks in advance.
Here is the project GitHub link.
Here is the urls.py of the app (named as "account")
urlpatterns = [
#path('login/', views.user_Login, name='login'),
# Log-in and Logout
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('',dashboard,name='dashboard'),
# change password urls
path('password_change/',auth_views.PasswordChangeView.as_view(),name='password_change'),
path('password_change/done/',auth_views.PasswordChangeDoneView.as_view(),name='password_change_done'),
# reset password urls
path('password_reset/',auth_views.PasswordResetView.as_view(),name='password_reset'),
path('password_reset/done/',auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(),name='password_reset_confirm'),
path('reset/done/',auth_views.PasswordResetCompleteView.as_view(template_engine='accounts/password_change_form.html'),name='password_reset_complete'),
]
settings.py
INSTALLED_APPS = [
'account.apps.AccountConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',]
LOGIN_REDIRECT_URL = 'dashboard'
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
templates/registration/login.html
{% extends "base.html" %}
{% block title %}Log-in{% endblock %}
{% block content%}
<h1>Log-in</h1>
{% if form.errors %}
<p>
Your username and passwoed didn't match.
Please try again.
</p>
{% else %}
<p>Please, use the following form to log-in:</p>
{% endif %}
<div class="login-form">
<form action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<p>Forgotten your password? </p>
<input type="hidden" name="next" value="{{ next }}">
<p><input type="submit" value="Log-in"></p>
</form>
</div>
{% endblock %}
templates/registration/logged_out.html
{% extends "base.html" %}
{% block title %}Logged out{% endblock %}
{% block content %}
<h1>Logged out</h1>
<p>
You have successfully logged out.
You can log-in-again
</p>
{% endblock %}
templates/registration/password_change_done.html
{% extends "base.html" %}
{% block title %}Logged out{% endblock %}
{% block content %}
<h1>Logged out</h1>
<p>
You have successfully logged out.
You can log-in-again
</p>
{% endblock %}
templates/registration/password_change_form.html
{% extends "base.html" %}
{% block title %}Change your password{% endblock %}
{% block content %}
<h1>Change your password</h1>
<p>Use the form below to change your password.</p>
<form method="post">
{{ form.as_p }}
<p><input type="submit" value="Change"></p>
{% csrf_token %}
</form>
{% endblock %}
templates/registration/password_reset_ confirm.html
{% extends "base.html" %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<h1>Reset your password.</h1>
{% if validlink %}
<p>Please enter your new password twice:</p>
<form method="post" action="password_change_form.html">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Change my password"></p>
</form>
{% else %}
<p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
{% endif %}
{% endblock %}
templates/registration/password_reset_complete.html
{% extends "base.html" %}
{% block title %}Password reset{% endblock %}
{% block content %}
<h1>Password set</h1>
<p>Your password has been set. You can log in now </p>
{% endblock %}
templates/registration/password_reset_done.html
{% extends "base.html" %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<h1>Reset your password</h1>
<p>We've emailed you instruction for setting your password.</p>
<p>If you don't receieve an email, please make sure you've enter the address you reginstered with.</p>
{% endblock %}
templates/registration/password_reset_email.html
Someone asked for password resent for email {{ email }}. Follow the link below:
{{ protocol }}://{{ domain }}{% url "password_reset_confirm" uidb64=uid token=token %}
Your username, in case you've forgotten: {{ user.get_username }}
templates/registration/password_reset_form.html
{% extends "base.html" %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<h1>Forgotten your password ?</h1>
<p>Enter your e-mail address to obtain a new password.</p>
<form method="post">
{{ form.as_p }}
<p><input type="submit" value="Sent e-mail"></p>
{% csrf_token %}
</form>
{% endblock %}
Email Screenshot.
enter image description here
This link open to this page
enter image description here
The Problem I can think of is that Django doesn't know where to search for reset password HTML files, that's why is it showing the default ones.
To override the default, you need to pass template_name to theas_view so that it knows what to render. One example is given below.
path('reset_password/',
auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html.html'),
name='reset_password'),

404 Page not found Django even though it is in urls.py

I don't know why I am getting a 404 error on the page flights/1 when clearly, I have a url pattern for
<int:flight_id> (flights is a app). Help!
My urls.py for the project
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('flights/', include('flights.urls'))
]
My urls.py for the flights app
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<int:flight_id>", views.flight, name="flight"),\
]
And the views.flight
def flight(request, flight_id):
flight = Flight.objects.get(id=flight_id)
return render(request, "flights/flight.html", {
"flight": flight,
"passengers": flight.passengers.all(),
"non_passengers": Passenger.objects.exclude(flights=flight).all()
})
Template
{% extends "flights/layout.html" %}
{% block body %}
<h1>Flight {{ flight.id }}</h1>
<ul>
<li>Origin: {{ flight.origin }}</li>
<li>Destination: {{ flight.destination }}</li>
<li>Duration: {{ flight.duration }}</li>
</ul>
<h2>Passengers</h2>
<ul>
{% for passenger in passengers %}
<li>{{ passenger }}</li>
{% empty %}
<li>No passengers.</li>
{% endfor%}
</ul>
<h2>Add Passenger</h2>
<form method="POST" action="{% url 'flight' flight.id book %}">
{% csrf_token %}
<select name="passengers">
{% for passenger in non_passengers %}
<option value="{{ passenger.id }}">{{ passenger }}</option>
{% endfor %}
</select>
</form>
Back to Flight List
{% endblock %}
With layout.html being html boilerplate code

Unclosed for tag in django. Looking for one of: empty, endfor

The error references {% for film in form %} not being closed in: form-template.html:
{% for field in form %}
{{field.errors}}
<label>{{ field.label_tag }}</label>
{{ field }}
{$ endfor %}
My comment_form.html:
<form action="" method="post">
{$ csrf_token %}
{% include 'films/form-template.html' %}
<button type="submit">Submit</button>
</form>
urls.py:
app_name = 'films'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
# path('<int:film_id>/comment', views.add_comment, name='add_comment'),
path('<int:pk>', views.DetailView.as_view(), name='detail'),
path('<int:film_id>/add/$', views.CommentCreate.as_view(), name='add_comment'),
]
You have an error in the end tag of your for loop.
{$ endfor %}
Should be
{% endfor %}

Reverse for '' with arguments '(1L,)' and keyword arguments '{}' not found

I'm new to Django and faced with next problem: when I turn on the appropriate link I get next error:
NoReverseMatch at /tutorial/
Reverse for 'tutorial.views.section_tutorial' with arguments '(1L,)' and keyword arguments '{}' not found.
What am I doing wrong? and why in the args are passed "1L" instead of "1"? (when i return "1" i get same error.) I tried to change 'tutorial.views.section_tutorial' for 'section-detail' in my template but still nothing has changed. Used django 1.5.4, python 2.7; Thanks!
tutorial/view.py:
def get_xhtml(s_url):
...
return result
def section_tutorial(request, section_id):
sections = Section.objects.all()
subsections = Subsection.objects.all()
s_url = Section.objects.get(id=section_id).content
result = get_xhtml(s_url)
return render(request, 'tutorial/section.html', {'sections': sections,
'subsections': subsections,
'result': result})
tutorial/urls.py:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
url(r'^$', views.main_tutorial, name='tutorial'),
url(r'^(?P<section_id>\d+)/$', views.section_tutorial, name='section-detail'),
url(r'^(?P<section_id>\d+)/(?P<subsection_id>\d+)/$', views.subsection_tutorial, name='subsection-detail'),
)
urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^tutorial/$', include('apps.tutorial.urls')),
)
main.html:
{% extends "index.html" %}
{% block content %}
<div class="span2" data-spy="affix">
<ul id="menu">
{% for section in sections %}
<li>
{{ section.name }}
<ul>
{% for subsection in subsections%}
{% if subsection.section == section.id %}
<li><a href=#>{{ subsection.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</li>
</ul>
</div>
<div class="span9">
<div class="well">
{% autoescape off%}
{{ result }}
{% endautoescape %}
</div>
</div>
{% endblock %}
You don't need $ identifier in url regex in your main urls file when including app urls:
url(r'^tutorial/$', include('apps.tutorial.urls')),
should be:
url(r'^tutorial/', include('apps.tutorial.urls')),

Link a view to a link Django

I am making a simple blog app in Django as a learning exercise. I am able to add posts, see them all on the front page, so it is working so far. Where I am having an issue is creating a view that shows the whole post on a separate page. I want to click the title, and go to a page at the url /post/primary key that has the title and body. When I click on the link, I get a page with just the base.html. Not sure where I am missing anything, here are urls.py, views.py, and post.html:
urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('blog.views',
url(r'^$', 'frontpage'),
url(r'^post/(\d+)/$', 'post'),
)
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
)
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render_to_response
from blog.models import *
views.py
def frontpage(request):
posts = Post.objects.all().order_by("-created")
paginator = Paginator(posts, 5)
page = request.GET.get('page', '1')
try:
posts = paginator.page(page)
except (InvalidPage, EmptyPage):
posts = paginator.page(paginator.num_pages)
return render_to_response("list.html", dict(posts=posts, user=request.user))
def post(request, pk):
"""Single Post"""
post = Post.objects.get(pk = pk)
d = dict(post=post, user=request.user)
return render_to_response("post.html", d)
post.html
{% extends "base.html" %}
{% block content %}
<div class ="main">
<ul>
{% for post in posts.object_list %}
<div class = "title">{{ post.title }}</div>
<ul>
<div class="time"> {{ post.created }}</div>
<div class ="body"> {{ post.body|linebreaks }}</div>
</ul>
{% endfor %}
</ul>
</div>
{% endblock %}
Thanks in advance for your help.
I am assuming the page.html is actually the post.html you have in yoru codes sample???You no longer have a collection of posts but instead just have 1 post
This needs to change from: (which is looping through your posts)
{% extends "base.html" %}
{% block content %}
<div class ="main">
<ul>
{% for post in posts.object_list %}
<div class = "title">{{ post.title }}</div>
<ul>
<div class="time"> {{ post.created }}</div>
<div class ="body"> {{ post.body|linebreaks }}</div>
</ul>
{% endfor %}
</ul>
</div>
{% endblock %}
To something like (which just displays your single post):
{% extends "base.html" %}
{% block content %}
<div class ="main">
{{ post.title }}
{{ post.created }}
{{ post.body }}
</div>
{% endblock %}
You have to change urls.py to go to a page at the url /post/primary key.
urlpatterns = patterns('blog.views',
url(r'^$', 'frontpage'),
url(r'^post/(?P<pk>\d+)/$', 'post'),
)

Categories

Resources