Django version 2.0
views.py
from django.views import generic
from .models import Album
class IndexView(generic.ListView):
template_name = 'music/index.html'
context_object_name = 'all_albums'
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model = Album
template_name = 'music/details.html'
details.html
{% extends 'music/base.html' %}
{% block body %}
<!-- {{album}} -->
<img src="{{album.album_logo}}">
<h1>{{album.album_title}}</h1>
<h3>{{album.artist}}</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'music:favorite' album.id %}" method="post">
{% csrf_token %}
{% for song in album.song_set.all %}
<input type="radio" id="song{{ forloop.counter }}" name="song" value="{{ song.id }}">
<label for="song{{ forloop.counter }}">
{{ song.song_title }}
{% if song.is_favorite %}
<img src="http://i.imgur.com/b9b13Rd.png" />
{% endif %}
</label><br>
{% endfor %}
<input type="submit" value="Favorite">
</form>
{% endblock %}
index.html
{% extends 'music/base.html' %}
{% block body %}
{% if all_albums %}
<h3>Here are all my Albums:</h3>
<ul>
{% for album in all_albums %}
<li>{{ album.album_title }}</li>
{% endfor %}
</ul>
{% else %}
<h3>You don't have any albums</h3>
{% endif %}
{% endblock %}
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Viberr</title>
{% load staticfiles %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type="text/css">
<link rel="stylesheet" type="text/css" href="{% static 'music/style.css' %}" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Header -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#topNavBar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{% url 'music:index' %}">University of Calicut</a>
</div>
<!-- Items -->
<div class="collaps navbar-collaps" id="topNavBar">
<ul class="nav navbar-nav">
<li class="active">
<a href="{% url 'music:index' %}">
<span class="glyphicon glyphicon-cd" aria-hidden="true"></span>
Albums
</a>
</li>
<li class="">
<a href="{% url 'music:index' %}">
<span class="glyphicon glyphicon-music" aria-hidden="true"></span>
Songs
</a>
</li>
</ul>
<form class="navbar-form navbar-left" role="search" method="GET" action="#">
<div>
<input type="text" class="form-control" name="q" value="">
<button type="submit" class="btn btn-default">Search</button>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="#">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
Add Album
</a>
</li>
<li class="">
<a href="#">
<span class="glyphicon glyphicon-off" aria-hidden="true"></span>
Logout
</a>
</li>
</ul>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
</html>
urls.py (music)
from django.conf.urls import url
from .import views
from django.urls import path
app_name = 'music'
urlpatterns = [
url(r'^$',views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='details'),
]
models.py
from django.db import models
class Album(models.Model):
artist = models.CharField(max_length=250)
album_title=models.CharField(max_length=500)
genre=models.CharField(max_length=100)
album_logo=models.CharField(max_length=1000)
def __str__(self):
return self.album_title + ' - ' + self.artist
class Song(models.Model):
album = models.ForeignKey(Album, on_delete=models.CASCADE)
file_type = models.CharField(max_length=10)
song_title = models.CharField(max_length=250)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.song_title
I attached images of my home page and linked page,
please help me regarding the issue
n the above code, the second page is throwing me a error
django.urls.exceptions.NoReverseMatch: Reverse for 'favorite' not found. 'favorite' is not a valid view function or pattern name**
Please someone help me out where im wrong! Thanks in advance!
I can't see any url with the name favorite here, which is what the error is telling you as well:
app_name = 'music'
urlpatterns = [
url(r'^$',views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='details'),
]
you probably copy pasted the details.html or you simly forgot to specify the "favorite" url in the app "music"
EDIT:
As you have clarified that you are unable to solve the problem on your onw:
Change details.html as such:
<form action="{% url 'music:favorite' album.id %}" method="post">
replace it with
<form action="#" method="post">
you're site should then at least load.
EDIT2:
After watchich the youtube video you linked in your comment:
all the answers are in the video and its comments!
You didn't update details.html. Watch closely around min 1:50.
Btw my solution of replacing the form action will work as well, but the output will be slightly different from what you might expect...
I hope this will get you started ;)
Related
I am new to Django and am trying to make an online journal, and the server runs fine, but when I press the create button, it does not redirect to the form even though a POST request is being sent. screenshot of the create page. I'm not sure why.
This is the code:
views.py:
from django.shortcuts import render, redirect, get_object_or_404
from ejournal.models import Journal
from ejournal.forms import JournalForm
def make_entry(request):
if request.method == "POST":
entry = JournalForm(request.POST, request.FILES)
if entry.is_valid():
entry.save()
return redirect('list_journals')
else:
entry = JournalForm()
context = {
'entry':entry
}
return render(request, 'ejournal/create.html', context)
def delete_entry(request, id):
entry = get_object_or_404(Journal, id=id)
if request.method == "POST":
entry.delete()
return redirect('list_journals')
context = {
'object':object
}
return render(request, 'journal/delete.html',context)
def list_journals(request):
entries = Journal.objects.all()
context = {
'entries': entries
}
return render(request, 'ejournal/list_journals.html',context)
def journal_detail(request, id):
journal = Journal.objects.get(id=id)
context = {
'journal':journal
}
return render(request, 'journal/journal_detail.html', context)
forms.py
from ejournal.models import Journal
from django.forms import ModelForm
class JournalForm(ModelForm):
class Meta:
model = Journal
fields = ['name','title','text','image']
models.py
from django.db import models
class Journal(models.Model):
name = models.CharField(max_length=20)
title = models.CharField(max_length=50)
text = models.TextField(max_length=1000)
date = models.DateField(auto_now_add=True)
image = models.FileField(upload_to='')
archived = models.BooleanField(default=False)
def __str__(self):
return self.name
base.html
{% load static %}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>My Online Journal</title>
<!-- Bootstrap core CSS -->
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
<link href="{% static 'css/style.css' %}" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="{% url 'list_journals' %}">Smart Journal</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="btn btn-outline-info btn-md" href="{% url 'list_journals' %}">LIST ALL</a>
</li>
</ul>
</div>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="btn btn-outline-info btn-md" href="{% url 'make_entry' %}">+ New Journal Entry </a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Page Content -->
<div class="container" id="app">
{% block content %}
{% endblock %}
</div>
<!-- /.container -->
<!-- Bootstrap core JavaScript -->
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
</body>
</html>
create.html
{% extends 'ejournal/base.html' %}
{% block content %}
<div class="center_journal">
<h1> Create Journal Entry </h1>
</div>
<div class="center_journal">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.as_p }}</p>
<button class="btn btn-primary" type="submit">CREATE</button>
</form>
</div>
{% endblock content %}
urls.py
from django.urls import path
from ejournal.views import make_entry, delete_entry, list_journals, journal_detail
urlpatterns = [
path('create', make_entry, name="make_entry"),
path('list_journals', list_journals, name="list_journals"),
path('delete/<int:id>/', delete_entry, name="delete_entry"),
path('journal_detail/<int:id>',journal_detail, name="journal_detail"),
]
You pass the form as the variable entry to your template ejournal/create.html.
Inside this template you use {{ form.as_p }}, but form is undefined. You need to use the variable entry because thats where the form is assigned to.
So your create.html becomes this:
{% extends 'ejournal/base.html' %}
{% block content %}
<div class="center_journal">
<h1> Create Journal Entry </h1>
</div>
<div class="center_journal">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ entry.as_p }}</p>
<button class="btn btn-primary" type="submit">CREATE</button>
</form>
</div>
{% endblock content %}
You're checking if your form is valid by using entry.is_valid(), but your form will always be invalid because you didn't pass the correct form in your template, as described above.
Check with form's action='' attribute like this...
<form method="POST" action="{% url 'make_entry' %}" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.as_p }}</p>
<button class="btn btn-primary" type="submit">CREATE</button>
</form>
I am writing a website on Flask, and I encountered a problem that when I send an email with a link to a password recovery page, the http address of which has a token that changes every time, the template that I connected is not generated at all. with completely wrong css styles.
Although the same template is connected in the same way to other pages and everything is fine, what is needed is generated
Here is my view function to handle this page
#app.route('/reset_password/<token>', methods=['POST', 'GET'])
def reset_token(token):
if current_user.is_authenticated:
return redirect(url_for('index_page'))
user = User.verify_reset_token(token)
if user is None:
flash('Неправильний,або застарілий токен', category='error')
return redirect(url_for('reset_password'))
form = ResetPasswordForm()
if form.validate_on_submit():
hash_password = generate_password_hash(form.password.data)
user.password = hash_password
db.session.commit()
return render_template('reset_password.html', title='Відновлення паролю', form=form,
css_link=css_file_reset_password_request_page)
For comparison, here is a function that connects to the same css-style
#app.route('/reset_password', methods=['POST', 'GET'])
def reset_password():
if current_user.is_authenticated:
return redirect(url_for('index_page'))
form = RequestResetForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is None:
flash('Неправильний email', category='error')
else:
send_reset_email(user)
flash('Повідомлення було надіслано на вашу електронну пошту,зайдіть туди,щоб отримати '
'інструкції', category='success')
return render_template('reset_password_request.html', title='Відновлення паролю', form=form,
css_link=css_file_reset_password_request_page)
But as a result, these pages have a completely different look
Here is the html template of this page(It is the same as reset_password_request from the previous function, only with some changes)
{% extends 'base.html' %}
{% block body %}
{{ super() }}
{% for cat, msg in get_flashed_messages(True) %}
<div class="flash {{cat}}">{{msg}}</div>
{% endfor %}
<div class="container">
<form class="box" method="POST">
{{ form.hidden_tag() }}
<h1 title="Будь ласка,придумайте новий надійний
пароль">Оновлення паролю</h1>
<div class="group">
<label>{{ form.password.label }}</label>
{{ form.password }}
</div>
<div class="group">
<label>{{ form.double_password.label }}</label>
{{ form.double_password }}
</div>
<div class="group">
<center><button>Відновити</button></center>
</div>
</form>
</div>
{% endblock %}
THIS IS MY base.html FILE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ css_link }}" type="text/css"/>
<script src="https://kit.fontawesome.com/b8991598b2.js"></script>
{% block title %}
{% if title %}
<title>{{ title }}</title>
{% else %}
<title>Сайт</title>
{% endif %}
{% endblock %}
</head>
<body>
<div class="content">
<header class="p-3 bg-dark text-white">
<div class="container">
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
<a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-white text-decoration-none">
<svg class="bi me-2" width="40" height="32" role="img" aria-label="Bootstrap"><use xlink:href="#bootstrap"></use></svg>
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li>Home</li>
<li>Features</li>
<li>Pricing</li>
<li>FAQs</li>
<li>About</li>
</ul>
<div class="search-box">
<input class="search-txt" type="text" placeholder="Search...">
<a class='search-btn' href="#">
<i class="fas fa-search"></i>
</a>
</div>
<div class="text-end">
Sign-up
Login
Logout
</div>
</div>
</div>
</header>
{% block body %}
{% endblock %}
</div>
<footer class="footer">
<ul class="nav justify-content-center border-bottom pb-3 mb-3">
<li class="nav-item">Home</li>
<li class="nav-item">Features</li>
<li class="nav-item">Pricing</li>
<li class="nav-item">FAQs</li>
<li class="nav-item">About</li>
</ul>
</footer>
</body>
</html>
And this is my connect to css_files in main.py
css_file = 'static/css/main.css'
css_file_authorization = 'static/css/login_form.css'
css_file_reset_password_request_page = 'static/css/request_passsword.css'
But here is an example of a page that is generated after switching from email:
And here is the one that should have been generated, and is generated in the case of the second handler function
Maybe someone knows how to solve it
Using Django tutorial, I built a basic ModelForm with choice fields:
One field (Shirt size) - is not showing choices
Second field - not showing at all
I can't find the reason, anywhere I checked it seems like I'm doing things right, but obviously I'm not.
Thank you.
The Result Form
views.py
def person(request):
if request.method == 'POST':
form = forms.PersonForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = forms.PersonForm()
return render(request, 'development/form_template.html', {'form': form})
forms.py
class PersonForm(ModelForm):
class Meta:
model = models.Person
fields = '__all__'
models.py
class Person(models.Model):
SHIRT_SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60, default='Anonymous', help_text='Type your name.')
shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
medal_type = models.TextChoices('MedalType', 'GOLD SILVER BRONZE')
form_template.html
{% extends 'development/development_template.html' %}
{% block content %}
<div class="container">
<form action="/your-name/" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
</div>
{% endblock content %}
CSS files I'm using
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link type="text/css" rel="stylesheet" href="{% static 'css/style.css' %}">
The problem was 100% the support of Materializecss design in select - for some reason it's not supporting select anymore.
I used bootstrap4 to execute the form pages:
(1) install: pip install django-bootstrap4
(2) add to INSTALLED_APPS = [..., 'bootstrap4',]
(3) edit your HTML form
Here's html for example:
{% extends 'adolim/adolim_template.html' %}
{% block content %}
{% load bootstrap4 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full' %}
{# Display django.contrib.messages as Bootstrap alerts #}
{% bootstrap_messages %}
<div class="container" style="width:100%;" dir="rtl">
<div class="col s3 right">
</div>
<div class="col s6 right" dir="rtl">
<div class="container">
<div class="row"><br></div>
<h4 class="right">{{ page_title }}</h4>
<div class="row"><br></div>
<form dir="rtl" action="{% url 'adolim:add_order' %}" method="post" >
{% csrf_token %}
{% bootstrap_form form %}
<div class="col center">
{% buttons %}
<button class="btn-large waves-effect waves-light blue darken-1" type="submit" name="action">{{ page_title }} למערכת
<i class="material-icons right">send</i>
</button>
{% endbuttons %}
</div>
</form>
</div>
</div>
</div>
{% endblock content %}
The code was properly working before bootstrap theme, but now the book name and image are not displayed.
base.html file:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static '/books/style.css' %}" >
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<a class="navbar-brand" href="{% url 'books:index' %}">The Bookstore</a>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="">
<a href="{% url 'books:index' %}">
<span class="glyphicon glyphicon-book" aria-hidden="true"></span> Books
</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="{% url 'books:book-add' %}">
<span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> Add Book
</a>
</li>
<li class="">
<a href="{% url 'books:index' %}">
<span class="glyphicon glyphicon-log-out" aria-hidden="true"></span> Logout
</a>
</li>
</ul>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
</html>
The following HTML file is supposed to display the book name and image, but instead it displays {{book.name}} and it doesn't display book_image. The database is created and the book stored, but it is not displaying.
index.html file:
{% extends 'books/base.html' %}
{% block body %}
<link rel="stylesheet" href="books/style.css">
<div class=" col-md-12 jumbotron">
<h1>The Bookstore</h1>
<p>Collection of all popular books</p>
</div>
<div class=" col-md-10 jumbotron jumbotron-special" name="fig">
<div class="col-md-12 span-2">
<h2>Popular Books</h2>
</div>
</div>
<ul>
{% for book in object_list %}
<div class="col-lg-3 col-md-4 col-sm-6 " >
<div class = "thumbnail">
<img src = "{{book.book_image}}" alt = "Generic placeholder thumbnail">
<div class = "caption">
<h3>{{book.name}}</h3>
<p>{{book.author}}</p>
<p>
<a href = "{% url 'books:detail' book.id %}" class = "btn btn-primary" role = "button">
Details
</a>
<a href = "#" class = "btn btn-danger" role = "button">
Delete
</a>
</p>
</div>
</div>
</div>
{% endfor %}
</ul>
{% endblock %}
This is the view file where the functions are defined:
from django.views import generic
from .models import Book
from django.views.generic.edit import CreateView
class IndexView(generic.ListView):
template_name = 'books/index.html'
def get_queryset(self):
return Book.objects.all()
class BookCreate(CreateView):
model = Book
fields = ['name', 'author', 'price', 'types', 'book_image']
class DetailView(generic.DetailView):
model = Book
template_name = 'books/detail.html
So, I've been trying to create a user login. When I click submit on my login form, I get this error:
Exception Type: TypeError
Exception Value: 'function' object is not iterable
Here's the full Traceback: http://dpaste.com/3D1B7MG
So, If I'm reading the Traceback correctly, the problem is in the {% extends "base.html" %} line of all_poss.html. So would that would mean the problem is actually inside base.html? or in the view that controls all_posts?
My all_posts.html
{% extends "base.html" %}
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style></style>
<title></title>
</head>
<body>
</body>
</html>
base.html
{% load staticfiles %}
{% load crispy_forms_tags %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/base.css' %}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
<TITLE>{% block title %}{% endblock %}</TITLE>
</HEAD>
<BODY>
{% block content %}
<div class="navbar-wrapper">
<div class="post_button" style="width:58px; margin:0 auto;">
Submit a Post
</div> <!-- /.post_button-->
<div class="log_bar">
<ul>
{% if user.is_authenticated %}
<li>Welcome,</li>
<li>{{ user.username }}</li>
<li>|</li>
<li>Log Out</li>
{% else %}
<li>Please</li>
<li><a data-toggle="modal" data-target="#modal-login" href="">log in</a></li>
<li>or</li>
<li><a data-toggle="modal" data-target="#modal-register" href="">sign up</a></li>
{% endif %}
</ul>
</div><!-- /.log_bar -->
<nav class="navbar navbar-fixed-left navbar-static-top">
<div class="container-fluid">
<!-- Collect the nav links, forms, and other content for toggling -->
<ul class="nav navbar-nav ">
<li class="active">Home <span class="sr-only">(current)</span></li>
<li>All</li>
<li>New</li
<li class="dropdown">
Top<span class="caret"></span>
<ul class="dropdown-menu">
<li>hour</li>
<li>24 hrs</li>
<li>week</li>
<li>month</li>
<li>year</li>
<li>beginning of time</li>
<li role="separator" class="divider"></li>
<li>Custom Search</li>
</ul>
</li>
</ul>
</div><!-- /.container-fluid -->
</nav>
<div id="side_bar">
<form class="navbar-form navbar-static-top navbar-right" role="search" id="navBarSearchForm">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default" id="search_btn">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
</div><!-- /.side-bar -->
<button class="btn-block" id='hideshow' value='hide/show' style="display: block; height: 100%;"></button>
{% include 'register.html' %}
{% include 'login.html' %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery.leanModal.js"></script>
{% endblock %}
</BODY>
</HTML>
views.py
def login(request):
"""
Log in view
"""
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
if user.is_active:
django_login(request, user)
return render(request, 'all_posts.html', {'user': request.user})
else:
form = AuthenticationForm()
return render_to_response('login.html', {
'authenticationform': form,
}, context_instance=RequestContext(request))
def all_posts(request):
post_list = TextPost.objects.all().order_by('-score'))
paginator = Paginator(post_list, 100) # Show 100 contacts per page
registrationform = RegistrationForm(request.POST or None)
authenticationform = AuthenticationForm(request.POST or None)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
posts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
posts = paginator.page(paginator.num_pages)
return render(request, 'all_posts.html', {'posts': posts, 'registrationform': registrationform, 'authenticationform': authenticationform, 'user': request.user})
Edit 1: login.html login.html and register.html are identical modals, one with a login form and the other with the registration one.
{% load staticfiles %}
{% load crispy_forms_tags %}
<div class="modal" id="modal-login">
<div class="modal-dialog">
<div class="modal-content">
<form enctype="multipart/form-data" method="post" action="login/">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Log In</h3>
</div>
<div class="modal-body">
{% csrf_token %}
{{ authenticationform|crispy }}
</div>
<div class="modal-footer">
<input type='submit' class="btn btn-primary" value="Log In" />
</div>
</form>
</div>
</div>
</div>
Edit 2: register.html
{% load staticfiles %}
{% load crispy_forms_tags %}
<div class="modal" id="modal-register">
<div class="modal-dialog">
<div class="modal-content">
<form enctype="multipart/form-data" method="post" action="register/">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Register</h3>
</div>
<div class="modal-body">
{% csrf_token %}
{{ registrationform|crispy }}
</div>
<div class="modal-footer">
<input type='submit' class="btn btn-primary" value="Register" />
</div>
</form>
</div>
</div>
</div>
Alright, I was able to solve it.
The problem was apparently in my base.html where I had:
<li>{{ user.username }}</li>
and
<li>Log Out</li>
and I just changed the href="{% %}" tags to "profile/" and "logout/"
respectively.
I ran into this error while doing the Django tutorial.
The core problem was in my app's urls.py, in urlpatterns, with the second argument here:
path('', views.index, views.IndexView.as_view(), name='index'),
It should have been:
path('', views.IndexView.as_view(), name='index'),
Not sure how the extra views.index snuck in there but it was causing the problem. (as to why that exact error occurs - not going to dig too deep)
I believe this is preferable to deleting the {% url '...' id %} call and hard-coding its result as advised in the solution above.