Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/accounts/register.html - django

Working on *django * on a test app, while creating a subpage as register in index/homepage, facing an error as follows:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
[name='index']
admin/
accounts
^media/(?P.*)$
The current path, accounts/register.html, didn’t match any of these.
views.py:
from django.shortcuts import render
def register(request):
return render(request,'register.html')
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("register",views.register, name="register")
]
mysite url.py:
from django.contrib import admin
from django.urls import include , path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',include('Sell.urls')),
path('admin/', admin.site.urls),
path('accounts', include('accounts.urls'))
]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
index.html:
<div class="container">
<div class="header_section_top">
<div class="row">
<div class="col-sm-12">
<div class="custom_menu">
<ul>
<li>Best Sellers</li>
<li>Gift Ideas</li>
<li>New Releases</li>
<li>Register</li>
<li>Customer Service</li>
</ul>
</div>
</div>
</div>
</div>
</div>
#Although the register.html is in a templates folder but i tried it too but still not working.
Register.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv='X-UA-Compatible' content="IE=edge">
<title>Register</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="main.css">
<script src="main.js"></script>
</head>
<body>
<form action='register' method='post'>
{% csrf_token %}
<input type="text" name="first_name" placeholder="First Name"><br/>
<input type="text" name="last_name" placeholder="Last Name"><br/>
<input type="text" name="username" placeholder="username"><br/>
<input type="Email" name="Email" placeholder="Email"><br/>
<input type="Password" name="Pasword1" placeholder="Pasword"><br/>
<input type="Password" name="Password2" placeholder="Confirm Pasword"><br/>
</form>
</body>
</html>

you should use the django template tag:
<li>Register</li>
which refers to the "name" in the path definition in urls.py

Related

How to generate a url that redirects to a specific view of another application

I'm working on a website, where I want to show all my django project.
So, I have a main app, where i'm generating template for all my projects
my home template when we access to the website:
<!doctype html>
{% load static %}
<html lang="fr">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="{% static 'home/css/style.css' %}">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Nerko+One&family=Roboto&display=swap&family=Open+Sans&display=swap" rel="stylesheet">
<meta name="viewport" content="width=device-width"/>
</head>
<body>
<header>
<h1></h1>
<span class="separator"></span>
<h2></h2>
<img src="{% static '/home/pictures/logo.png' %}" alt="logo" title="Ceci est mon logo" id="logo">
</header>
<nav>
<ul>
<li>Accueil</li>
<li>Projets</li>
<li>Contact</li>
</ul>
</nav>
<div class="contener">
<section>
<h3>Projets</h3>
<!-- conteneur -->
<div id="projects">
{% for projet in projets %}
<div class="project">
<a href="{% url 'projet-detail' projet.nom_lien %}">
<div class="picture">
<img src="{% static '' %}{{ projet.lien_image_presentation }}" alt="Présentation {{ projet.nom }}">
</div>
<span>{{ projet.nom }}</span>
<span class="date-project">{{ projet.date }}</span>
</a>
</div>
{% endfor %}
</div>
</section>
<section>
<h3>Contact</h3>
<div id="contact">
<p>Si vous désirez me contacter, n'hésitez pas à m'écrire à l'adresse <b>aymerick.cotche#hotmail.fr</b></p>
</div>
</section>
</div>
<footer>
<span>2020 © - Aymerick Cotche</span>
</footer>
</body>
</html>
template to see my project description :
{% load static %}
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>{{ projet.nom }} | {{ projet.auteur }}</title>
<link rel="stylesheet" type="text/css" href="{% static 'home/css/style.css' %}">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Nerko+One&family=Roboto&display=swap&family=Open+Sans&display=swap" rel="stylesheet">
<meta name="viewport" content="width=device-width"/>
</head>
<body>
<header>
<h1></h1>
<span class="separator"></span>
<h2>{{ projet.nom }}</h2>
</header>
<nav>
<ul>
<li>Accueil</li>
<li>Projets</li>
<li>Contact</li>
</ul>
</nav>
<div class="contener">
<section>
<h3>Introduction</h3>
<p> {{ projet.description }} </p>
</section>
<section id="production_project">
<h3>Production</h3>
<img src="{% static '' %}{{ projet.lien_image_production }}" alt="mon premier projet">
voir le projet
</section>
</div>
</body>
</html>
my urls file for this app:
from django.urls import path
from home import views
urlpatterns = [
path('', views.home),
path('<str:nom_lien>/', views.projet_detail, name='projet-detail'),
]
My views file for this app :
from django.shortcuts import render, redirect
from home.models import Projet
def home(request):
projets = Projet.objects.all().order_by('numero')
return render(request, 'home/index.html', {'projets': projets})
def projet_detail(request, nom_lien):
projet = Projet.objects.get(nom_lien=nom_lien)
context = {
'projet': projet
}
return render(request, 'home/projets_base.html', context)
That I want to do it's to have a link in my project description template, to go to the project, and use the project. The project it's another django app in the same project. and this link should be like "/nom-lien/voirprojet/"
For example in my app "pizza mama" I have this url and views file:
from django.urls import path
from . import views
app_name = "main"
urlpatterns = [
path('pizzamama/voirprojet/', views.index, name="index"),
]
The link have to use the Projet.nom_lien field in my database and redirect to the the project page.
Could someone help me please, I'm stuck and can't make it works.
you want to store the link in the database
define in models.py a class Project
class Project(models.Model):
link_to_app = models.CharField(max_length=50)
register it in the admin.py
in the admin panel enter the what you want Links
then you can access it like this in urls.py
from .models import Link
link = Projects.objects.get(pk=1)
my_link = link.link_to_app
then pass my_link to the url path
urlpatterns += [
path(my_link, views.index, name='index')),
]

Django NoReverseMatch at /register

I'm attempting to enable users to register on my Django based website; however I am running into the following error:
NoReverseMatch at /register
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
I am struggling to see what needs to be changed below, I feel that I have tried tweaking everything to get it working, but nothing seems to. I am using a namespace "Flightfinder" for my application, not sure if this has something to do with it?
Full Error:
NoReverseMatch at /register
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8001/register
Django Version: 3.1.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
Exception Location: /mnt/c/Users/tjmce/Desktop/Git/CS50w/Final Project/finalproject/.venv/lib/python3.8/site-packages/django/urls/resolvers.py, line 685, in _reverse_with_prefix
Python Executable: /mnt/c/Users/tjmce/Desktop/Git/CS50w/Final Project/finalproject/.venv/bin/python3
Python Version: 3.8.5
Python Path:
['/mnt/c/Users/tjmce/Desktop/Git/CS50w/Final Project/finalproject',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/mnt/c/Users/tjmce/Desktop/Git/CS50w/Final '
'Project/finalproject/.venv/lib/python3.8/site-packages']
Server time: Wed, 11 Nov 2020 20:32:27 +0000
Layout Template:
{% load static %}
<!doctype html>
<html lang="en">
<head>
<!-- jQuery & jQuery UI -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<!-- Bootstrap (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Main CSS-->
<link rel="stylesheet" type="text/css" href="{% static 'flightfinder/styles.css' %}">
<!-- Main JS -->
<script src="{% static 'flightfinder/main.js' %}"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<title>{% block title %}Flight Finder{% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" href="{% url 'Flightfinder:index' %}">
<img src="https://i.ibb.co/TRBkPBZ/Flight-Finder-removebg-preview.png" width="166" height="42" class="d-inline-block align-top" alt="">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="{% url 'Flightfinder:index' %}">Flights <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="#">Hotels</a>
<a class="nav-item nav-link" href="#">Car Hire</a>
</div>
<div class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<a class="nav-item nav-link" href="{% url 'Flightfinder:logout' %}">Log Out</a>
<a class="nav-item nav-link" href="#">Account</a>
{% else %}
<a class="nav-item nav-link" href="{% url 'Flightfinder:login' %}">Log In</a>
<a class="nav-item nav-link" href="{% url 'Flightfinder:register' %}">Register</a>
{% endif %}
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
</html>
Views.py:
def register(request):
if request.method == "POST":
username = request.POST["username"]
email = request.POST["email"]
# Ensure password matches confirmation
password = request.POST["password"]
confirmation = request.POST["confirmation"]
if password != confirmation:
return render(request, 'flightfinder/register.html', {
"message": "Passwords must match."
})
# Attempt to create new user
try:
user = User.objects.create_user(username, email, password)
user.save()
except IntegrityError:
return render(request, 'flightfinder/register.html', {
"message": "Username already taken."
})
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, 'flightfinder/register.html')
Urls.py - Main:
urlpatterns = [
path('', include('flightfinder.urls', namespace='Flightfinder')),
path('admin/', admin.site.urls),
]
Urls.py - App:
app_name = 'flightfinder'
urlpatterns = [
path('', views.index, name='index'),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path('origin_airport_search/', views.origin_airport_search, name='origin_airport_search')
]
Thank you in advance for having a look into this for me, very much appreciated!

Django NoReverseMatch - Amadeus API

I'm attempting to use an API as part of a project I'm working on and am having some difficulty currently:
API
https://developers.amadeus.com/self-service/category/air/api-doc/airport-and-city-search
API - Python SDK
https://github.com/amadeus4dev/amadeus-python
Django API Guide
https://developers.amadeus.com/blog/django-jquery-ajax-airport-search-autocomplete
I have followed the instructions from the above guide; however, I am receiving the following error when trying to render the index page which contains the code:
Reverse for 'origin_airport_search' not found. 'origin_airport_search' is not a valid view function or pattern name.
Index Template:
{% extends "flightfinder/layout.html" %}
{% block body %}
<div id="mainhomepagediv">
<div class="container-fluid px-0" id="mainhomepagediv">
<div class="row mx-0">
<div class="col-12 px-0">
<img src="https://i.ibb.co/rHYzcyc/Landscape2.jpg" class="img-fluid w-100">
</div>
</div>
</div>
<div>
<h1 id="homepageh1">Where are you flying to?</h1>
<div id="homepagebox">
<div>
<form id="homepageform" method="POST">
<div class="row">
<div class="col">
<label id="homepageformlabel">From</label>
<input type="text" class="form-control" placeholder="Origin city" id="inputOrigin">
</div>
<div class="col pb-3">
<label id="homepageformlabel">To</label>
<input type="text" class="form-control" placeholder="Destination city">
</div>
</div>
<div class="row">
<div class="col">
<label id="homepageformlabel">Depart</label>
<input type="date" class="form-control" placeholder="Origin city">
</div>
<div class="col">
<label id="homepageformlabel">Return</label>
<input type="date" class="form-control" placeholder="Destination city">
</div>
<div class="col-6">
<label id="homepageformlabel">Class</label>
<select id="inputState" class="form-control">
<option selected>Economy</option>
<option>Business</option>
<option>First</option>
</select>
</div>
</div>
<div class="row">
<div class="col">
</div>
<div class="col-3 pt-3">
<button class="btn float-right" id="homepageformbutton" type="submit">Search flights</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
// Amadeus origin airport search
$(document).ready(function () {
$("#inputOrigin").autocomplete({
source: "{% url 'origin_airport_search' %}",
minLength: 1,
delay: 200,
});
});
</script>
{% endblock %}
Layout Template:
{% load static %}
<!doctype html>
<html lang="en">
<head>
<!-- jQuery & jQuery UI -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<!-- Bootstrap (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Main CSS-->
<link rel="stylesheet" type="text/css" href="{% static 'flightfinder/styles.css' %}">
<!-- Main JS -->
<script src="{% static 'flightfinder/main.js' %}"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<title>{% block title %}Flight Finder{% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" href="{% url 'flightfinder:index' %}">
<img src="https://i.ibb.co/TRBkPBZ/Flight-Finder-removebg-preview.png" width="166" height="42" class="d-inline-block align-top" alt="">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="{% url 'flightfinder:index' %}">Flights <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="#">Hotels</a>
<a class="nav-item nav-link" href="#">Car Hire</a>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
</html>
Main - urls.py:
"""finalproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('flightfinder.urls', namespace='Flightfinder')),
path('admin/', admin.site.urls),
]
App - urls.py:
from django.urls import path
from . import views
app_name = 'flightfinder'
urlpatterns = [
path('', views.index, name='index'),
path('origin_airport_search/', views.origin_airport_search, name='origin_airport_search')
]
views.py:
import json
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib import messages
from amadeus import Client, ResponseError, Location
from .models import Airport, Flight, Passenger
amadeus = Client(client_id='removed',
client_secret='removed',
log_level='debug')
# Create your views here.
def index(request):
return render(request, 'flightfinder/index.html')
def origin_airport_search(request):
if request.is_ajax():
try:
data = amadeus.reference_data.locations.get(keyword=request.GET.get('term', None), subType=Location.ANY).data
except ResponseError as error:
messages.add_message(request, messages.ERROR, error)
return HttpResponse(get_city_airport_list(data), 'application/json')
def get_city_airport_list(data):
result = []
for i, val in enumerate(data):
result.append(data[i]['iataCode']+', '+data[i]['name'])
result = list(dict.fromkeys(result))
return json.dumps(result)
I have had a look through some similar stack queries, but none of them have helped thus far. Would be grateful if somebody had any ideas to try here?
Thank you!
You have set a namespace "Flightfinders" in your include. This means you have to use this namespace to call your URL:
source: "{% url 'Flightfinder:origin_airport_search' %}",

django create form with materialize modal

As far as I tried, I cannot get the form data or send it to the django view function through the materialize css modal. I'm just a novice in js, ajax and related.
The "CreateView" action for my form is running outside, but it is required to be inside a modal and still get the benefits of using django form validation, security, and so on. I'm using Django version 1.11.
The project has two applications: main that loads the main page, and library that performs actions on book objects. A book has only a title, and so there's a form defined like:
library/forms.py:
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = [
'title',
]
the project urls definition, mysite/urls.py :
from django.conf.urls import url, include
from django.contrib import admin
from main import views as main_views
urlpatterns = [
url(r'^$', main_views.home, name='home'),
url(r'^books/', include('library.urls')),
url(r'^admin/', admin.site.urls),
]
Home view, main/views.py:
from django.shortcuts import render
def home(request):
return render(request, 'main/home.html')
main/templates/main/home.html:
<html>
<head>
<title>A library</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
{% block body %}
<div class="container">
<!--a class="waves-effect waves-light btn modal-trigger" href="#modal1">Create Book</a-->
<a class="waves-effect waves-light btn modal-trigger" data-target="add_book_modal" href="{% url 'books:create' %}">
Create Book
</a>
<div class="divider"></div>
<div class="section">
<h5>Section 1</h5>
<p>Stuff</p>
</div>
</div>
{% endblock %}
{% include 'library/book_form.html' %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script>
$( document ).ready(function() {
$('.modal').modal();
});
</script>
</body>
</html>
The library's urls, library/urls.py:
from django.conf.urls import url
from .views import BookCreateView
app_name='books'
urlpatterns = [
url(r'^create/$', BookCreateView.as_view(), name='create'),
]
library/views.py:
from django.shortcuts import render
from django.views.generic import CreateView
from django.urls import reverse_lazy
from .models import Book
from .forms import BookForm
class BookCreateView(CreateView):
form_class = BookForm
template_name = 'library/book_form.html'
success_url = reverse_lazy('home')
library/templates/library/book_form.html:
<div id="add_book_modal" class="modal">
<div class="modal-content">
{% block body %}
<main>
<h5>Add book information</h5>
{% if form.errors.non_field_errors %}
{{ form.errors.non_field_errors }}
{% endif %}
<form method="POST">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.source.errors }}
{{ form.source }}
<!--Form content-->
{{form.as_p}}
<!--Form button-->
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
</button>
</form>
</main>
{% endblock %}
</div>
</div>
Any help will be appreciated.
Changing a href link and setting the place of html code where the content will be rendered
main/templates/main/home.html:
...
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Create Book</a>
...
<!-- Modal Structure -->
<div id="modal1" class="modal">
<div class="modal-content">
<!--Form content goes in here.-->
</div>
</div>
{% endblock %}
<!-- django include tag removed -->
Jquery to render the form in the modal through django's routes
main/templates/main/home.html:
<script>
$( document ).ready(function() {
$('.modal').modal();
$('.modal-content').load("{% url 'books:create' %}");
});
</script>
Finally, book form specifying the way back.
_library/templates/library/book_form.html_:
<h5>Add book information</h5>
{% if form.errors.non_field_errors %}
{{ form.errors.non_field_errors }}
{% endif %}
<form method="POST" action="{% url 'books:create' %}">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.source.errors }}
{{ form.source }}
<!--Form content-->
{{form.as_p}}
<!--Form button-->
<button class="btn waves-effect waves-light" type="submit" name="action">Submit form
</button>
</form>

Django, not able to load image, but css is loaded

I m able to load css file based on the tutorial provided by djangoproject but images are not getting loaded.
Here is the file structure:
appname/static/ mess/ img/ burger.jpg
Here is the login.html file where I m trying to load the image.
`<html>
<head>
<title>Login | Mess # IIIT Sri City</title>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'mess/css/style.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'mess/css/bootstrap.min.css' %}">
</head>
<body>
<div class="container">
<div id="left-content" class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
{% load staticfiles %}
<center><img src="{% static 'mess/img/burger.jpg' %}" alt="Please Login here"></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<form action="" method="post">{% csrf_token %}
{{ form}}
<input type="submit" value="Submit" />
</form>
</div>
</div>
</body>
`
It would be a great help if you could resolve me out of this issue.
PS: I m a beginner in Django.
Django version: 1.8
Please tell me if u need more info. Thanks in advance.
It was because the folder didn't have necessary permissions and Django was unable to access it.
in url.py have you configurated staticfiles?
if settings.DEBUG:
urlpatterns += patterns('',
#REMOVE IT in production phase
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT})
)
urlpatterns += staticfiles_urlpatterns()
There are a few changes to make here. You can copy and paste the following code below for your template, and the settings below that in your urls.py:
html:
{% load staticfiles %}
<html>
<head>
<title>Login | Mess # IIIT Sri City</title>
<link rel="stylesheet" type="text/css" href="{% static 'mess/css/style.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'mess/css/bootstrap.min.css' %}">
</head>
<body>
<div class="container">
<div id="left-content" class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><img src="{% static 'mess/img/burger.jpg' %}" alt="Please Login here" /></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<form action="" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
</div>
</div>
</body>
</html>
urls:
if settings.DEBUG:
urlpatterns += patterns('',) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('',) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In the html, I added {% load staticfiles %} at the top to keep your code DRY (Don't Repeat Yourself). I also made a few syntax changes to your code; very minor. In the urls, when your settings are set to DEBUG = True, Django will load your static images. When you go into production and DEBUG = False, Django no longer serves the static files. That is your server's responsibility.
I hope that helps.
If it still doesn't work, can you please post your STATIC_URL and STATIC_ROOT?