I am creating a login page without an inbuilt model form. I am using FormAPI of Django and followed all the steps to add a class attribute to the input fields of my form. But it still looks like a basic form without bootstrap.
forms.py:
#Login Form
class LoginForm(forms.Form):
username=forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
password=forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}))
Base template file to be inherited:
<!doctype html>
{% load static %}
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="{% static 'account/images/logo.ico' type='image/x-icon' %}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'account/css/style.css' %}">
<title>{% block title %}{% endblock title %}</title>
</head>
<body class="forbody">
<div class="container trp" style="margin-top: 2%; display: inline-block;">
<div class="row">
<div class="col-md-4">
<img src="{% static 'account/images/Logo.png' %}" alt="Logo" style="height: 60%; width: auto;">
</div>
</div>
</div>
<div class="container" style="width: 40%; margin-top: 10%;">
<div class="container" style="width: auto; margin-top: 8%;">
<div class="container" style="background-color: #696969; height: auto; width: auto; margin-top: 8%;">
<div class="row">
<div class="col-lg-6" style="padding-top: 25px; padding-bottom: 20px; padding-left: 20px;">
<form action="" method="post" novalidate>
{% csrf_token %}
{% block formcontent %} {% endblock formcontent %}
<div class="row text-center">
<div class="col-lg-12">
{% block loginbtn %} {% endblock loginbtn %}
</div>
</div>
<div class="row text-center">
<div class="col-lg-12" style="padding-top: 10px; padding-bottom: 12px;">
{% block outerlink %} {% endblock outerlink %}
{% block messages %} {% endblock messages %}
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.11.6/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
</body>
</html>
Actual login template:
{% extends 'account/forms_base.html' %}
{% block title %}Log-in{% endblock title %}
{% block formcontent %}
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
{{error}}
{% endfor %}
{% endif %}
{% for fm in form %}
{{fm.label_tag}} {{fm}} {{fm.error|striptags}} <br><br>
{% endfor %}
{% endblock formcontent %}
{% block loginbtn %}
<input class="submitbtn" type="submit" value="Login">
{% endblock loginbtn %}
{% block outerlink %}
<small style="margin-top: 5px;">To register Click Here</small>
{% endblock outerlink %}
{% block messages %}
{% if messages %}
{% for message in messages %}
<span {% if message.tags %} class="{{message.tags}}" {% endif %}>{{message}}</span>
{% endfor %}
{% endif %}
{% endblock messages %}
Where am I going wrong here? When I view the source page on the browser, the class attribute does not show at all!
I have in version of my software (settings.release=1.0.14) and I want to show this info in django admin header under or after the product logo, but no idea how to do it.
My base_site.html is:
{% extends "admin/base.html" %}
{% load static %}
{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name">
<a href="{% url 'admin:index' %}">
<img src="{% static 'lumbara.png' %}" height="50px" />
</a>
</h1>
{% endblock %}
{% block extrastyle %}
<style>
.module h2, .module caption, .inline-group h2,#header
{
/*margin: 0;*/
/*padding: 2px 2px 2px 2px;*/
/*font-size: 12px;*/
/*text-align: left;*/
/*font-weight: bold;*/
background: #006400 url(../img/default-bg.gif) top left repeat-x;
color: #fff;
}
</style>
{% endblock %}
{% block nav-global %}{% endblock %}
You have to write your own context_processors which returns a dictionary with your release variable. Then add it to the TEMPLATE settings, and you can use this variable in your templates - look at this answer for more info.
I have narrowed down to the following , if anyone can help me pointing out how i can convert the following into table view that would be awesome. Following html is extended from the base.html
{% block page_content %}
<h1>Projects</h1>
<div class="row">
{% for project in projects %}
<div class="col-md-4">
<div class="card mb-2">
<img class="card-img-top" src="{% static project.image %}">
<div class="card-body">
<h5 class="card-title">{{ project.title }}</h5>
<p class="card-text">{{ project.description }}</p>
<a href="{% url 'project_detail' project.pk %}"
class="btn btn-primary">
Read More
</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
Need help with displaying data in gridview . I m new to all this. I m able to read and display the data on a page from mongodb but need to display in a table.
million $ question for me now is where should i be adjusting the following snippet in my code to give me gridview
<table>
<tr>
<th>Field 1</th>
<th>Field N</th>
</tr>
{% for item in query_results %}
<tr>
<td>{{ item.field1 }}</td>
...
<td>{{ item.fieldN }}</td>
</tr>
{% endfor %}
</table>
My urls.py
from django.urls import path
from .import views
urlpatterns = [
path("",views.project_index, name = "project_index"),
path ("<project_detail>/",views.project_detail, name = "project_detail"),
#path("<int:pk>/", views.project_detail, name = "project_detail"),
]
My model.py
from django.db import models
# Create your models here.
class Project(models.Model):
title = models.CharField(max_length=100,primary_key=True)
desc = models.CharField(max_length=100)
urls = models.CharField(max_length=100)
#image = models.FilePathField(path="/img")
class Meta:
db_table = "spiderCollection1"
additional tables.py
import django_tables2 as tables
from .models import Project
import itertools
class ProjectTable(tables.Table):
class Meta:
model = Project
template_name = "django_tables2/bootstrap.html"
title = tables.Column("title")
desc = tables.Column("desc")
urls = tables.Column("urls")
following is views.py
from django_tables2 import SingleTableView
from django.shortcuts import render
from projects.models import Project
from projects.tables import ProjectTable
# Create your views here.
class ProjectListView(SingleTableView):
model = Project
table_class = ProjectTable
template_name = '/projects.html'
def project_index(request):
projects = Project.objects.all()
context = {
"projects":projects
}
return render (request, 'project_index.html',context)
#return render (request, 'project_index.html',locals())
def project_detail(request, pk):
#project = Project.objects.get(pk=pk)
project = Project.objects.all()
context = {
"project": project
#'personal_portfolio':project
}
return render(request, 'project_detail.html',context)
my main base.html
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="{% url 'project_index' %}">Portfolio</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'project_index' %}">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Blog</a>
</li>
</ul>
</div>
</div>
<!--<style>
h1 {
border: 5px solid red;
}
h2 {
border: 4px dotted blue;
}
div {
border: double;
}
</style>
-->
</nav>
<div class="container">
{% block page_content %}
{% endblock %}
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
and now project_index.html
{% extends "base.html" %}
{%load render_table from django_tables %}
{% load static %}
{% block page_content %}
<h1>Projects</h1>
<div class="row">
{% for project in projects %}
<div class="col-md-4">
<div class="card mb-2">
<img class="card-img-top" src="{% static project.image %}">
<div class="card-body">
<h5 class="card-title">{{ project.title }}</h5>
<p class="card-text">{{ project.description }}</p>
<a href="{% url 'project_detail' project.pk %}"
class="btn btn-primary">
Read More
</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
If the goal is to display a table without using the django-table2 package follow the first part of the answer. If the goal is to use django-table2 then jump to Part II:
Part I
Based on your colde, let us use the example snippet, and edit it in order to display your project data in an HTML table...
Starting with your project_index.html, we need create the skeleton of the html table using <table>, <thead>, <tbody> and <th> tags, and then to loop over the passed projects context variable and add entries to the table using the <td> tag. And because you are using the bootstrap framework, we will need the row and the column divss to display the table correctly.
{% extends "base.html" %}
{% load static %}
{% block page_content %}
<h1>Projects</h1>
<div class="row">
<div class="col-md-4">
<table class="table table-striped table-hover">
<thead>
<th>#</th>
<th>title</th>
<th>description</th>
</thead>
<tbody>
{% for project in projects %}
<td><strong>{{ forloop.counter }} </strong></td>
<td><strong>{{ project.title}</strong></td>
<td>{{ project.description} Read More </td>
{% endfor %}
</tbody>
</table>
<div>
</div>
{% endblock %}
Make sure that your url.py points to the function project_index
To learn more check:
https://www.w3schools.com/html/html_tables.asp
https://getbootstrap.com/docs/4.4/content/tables
https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#for
Part II
Django-table2 is a package that provides an app and middleware to generate html tables. In order to use it in your app make the following changes to your project_index.html:
{% extends "base.html" %}
{% load render_table from django_tables %}
{% load static %}
{% block page_content %}
<h1>Projects</h1>
<div class="row">
<div class="col-md-4">
{% render_table projects %}
</div>
</div>
{% endblock %}
The code above will use the html template provided by django-table2 for rendering tables, defined in your class ProjectTable
class ProjectTable(tables.Table):
class Meta:
model = Project
template_name = "django_tables2/bootstrap.html"
...
If you want to use a custom rendering you will need to set the template_name of the ProjectTable to your custom.html:
class ProjectTable(tables.Table):
class Meta:
model = Project
template_name = "custom.html"
...
Now create custom.html and add the code that will actually iterate over the items of the projects context variable. Maybe something like this (copied from the django_tables2/semantic.html)... Make changes you want to this template.
{% load django_tables2 %}
{% load i18n %}
{% block table-wrapper %}
<div class="ui container table-container">
{% block table %}
<table {% render_attrs table.attrs class="ui celled table" %}>
{% block table.thead %}
{% if table.show_header %}
<thead {{ table.attrs.thead.as_html }}>
<tr>
{% for column in table.columns %}
<th {{ column.attrs.th.as_html }}>
{% if column.orderable %}
{{ column.header }}
{% else %}
{{ column.header }}
{% endif %}
</th>
{% endfor %}
</tr>
</thead>
{% endif %}
{% endblock table.thead %}
{% block table.tbody %}
<tbody {{ table.attrs.tbody.as_html }}>
{% for row in table.paginated_rows %}
{% block table.tbody.row %}
<tr {{ row.attrs.as_html }}>
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
{% endblock table.tbody %}
{% block table.tfoot %}
<tfoot {{ table.attrs.tfoot.as_html }}>
{% if table.has_footer %}
<tr>
{% for column in table.columns %}
<td {{ column.attrs.tf.as_html }}>{{ column.footer }}</td>
{% endfor %}
</tr>
{% endif %}
{% block pagination %}
{% if table.page and table.paginator.num_pages > 1 %}
<tr>
<th colspan="{{ table.columns|length }}">
<div class="ui right floated pagination menu">
{% if table.page.has_previous %}
{% block pagination.previous %}
<a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}" class="icon item">
<i class="left chevron icon"></i>
</a>
{% endblock pagination.previous %}
{% endif %}
{% if table.page.has_previous or table.page.has_next %}
{% block pagination.range %}
{% for p in table.page|table_page_range:table.paginator %}
{% if p == '...' %}
{{ p }}
{% else %}
<a href="{% querystring table.prefixed_page_field=p %}" class="item {% if p == table.page.number %}active{% endif %}">
{{ p }}
</a>
{% endif %}
{% endfor %}
{% endblock pagination.range %}
{% endif %}
{% if table.page.has_next %}
{% block pagination.next %}
<a href="{% querystring table.prefixed_page_field=table.page.next_page_number %}" class="icon item">
<i class="right chevron icon"></i>
</a>
{% endblock pagination.next %}
{% endif %}
</div>
</th>
</tr>
{% endif %}
{% endblock pagination %}
</tfoot>
{% endblock table.tfoot %}
</table>
{% endblock table %}
</div>
{% endblock table-wrapper %}
Here make sure your urls.py includes:
...
path("projects/", ProjectListView.as_view())
...
for more refer to:
https://django-tables2.readthedocs.io/en/latest/pages/tutorial.html
https://github.com/jieter/django-tables2/blob/master/django_tables2/templates/django_tables2/semantic.html
<div class="center">
<table class="table table-bordered " border="1">
<tr>
<th>Name</th>
<th>Game</th>
</tr>
{% for i in user_data %}
<tr>
<td>{{i.name}}</td>
<td>{{i.favorite_game}}</td>
</tr>
{% endfor %}</table>
I am getting the following error
NoReverseMatch at /blog/index/
and
Reverse for 'feed_articles' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
just because I deleted/commented out a url I know longer need. The url isn't blog/index either. something tells me it's an ordering issue because I've had that happen before in my django app. But I am confused. Heres what I commented out
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^index/$', views.index, name='index'),
url(r'^video_submission/$', views.video_submission, name='submission'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^privacy/$', views.privacy, name='privacy'),
url(r'^dmca/$', views.dmca, name='dmca'),
url(r'^terms/$', views.terms, name='terms'),
url(r'^search/$', views.post_search, name='post_search'),
url(r'^create/$', views.post_create, name='create'),
url(r'^images/$', views.static_images, name='static_images'),
url(r'^video_agreement/$', views.video_agreement, name='video_agreement'),
### url(r'^feed_articles/$', views.articles_list, name='feed_articles'),
url(r'^feeds/$', views.feed_list, name='feed_list'),
url(r'^feeds/new$', views.new_feed, name='new_feed'),
url(r'^(?P<id>\d+)/feed_delete/$', views.feed_delete, name='feed_delete'),
url(r'^(?P<id>\d+)/article_delete/$', views.article_delete, name='article_delete'),
url(r'^tag/(?P<tag_slug>[-\w]+)/$', views.post_list,
name='post_list_by_tag'),
url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name='post_detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', views.post_update, name='update'),
url(r'^(?P<id>\d+)/delete/$', views.post_delete, name='delete'),
How can I correct my syntax so that this minor issue will work?
EDIT my view being called by blog/index
{% extends 'blog/base.html' %}
{% load staticfiles %}
<style>
{% block style %}
#hite{
min-height: 720px;
}
.post{
min-height: 157px;
max-height: 157px;
width: 100%;
}
.tall{
height: 300px;
}
{% endblock style %}
</style>
{% block content %}
<div id="hite">
<h1>worldstar</h1>
{% for d in divs %}
<div class="col-sm-6 col-md-4" style="margin-top: 30px">
<div class="thumbnail thumb tall">
<img src="{{d.src}}" alt="{{ d.text }}" class="img-responsive post">
<div class="caption">
<h5>{{ d.text }}</h5>
<p></p>
<p>World *</p>
</div>
</div>
</div>
{% endfor %}
{% for a in articles %}
<div class="col-sm-6 col-md-4" style="margin-top: 30px" >
<div class="thumbnail thumb tall">
<div class="caption">
<h4 style="height: 100px">{{a.title}}</h4>
<p>{{a.description|truncatechars:30 | safe}}</p>
<h4>From: {{a.feed|truncatechars:30}}</h4>
<p>
View Article
{% if user.is_authenticated %}
delete
{% endif %}
</p>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
my feed_articles
{% extends 'blog/base.html' %}
<style>
{% block style %}
/*ul{*/
/*list-style: none;*/
/*}*/
#hi{
min-height: 720px;
margin-top: 15px;
}
{% endblock style %}
</style>
{% block jumbotron %}
<h1>HArticles</h1>
<p> Have a look around at some of the latest news Here and abroad</p>
{% endblock jumbotron %}
{% block content %}
<div class="row" id="hi">
{% for a in articles %}
<div class="col-xs-12 col-sm-4 col-md-3 col-lg-6" >
<div class="thumbnail" style="height: 250px; padding-left: 10px">
<h4 style="height: 100px">{{a.title}}</h4>
<p>{{a.description|truncatechars:30 | safe}}</p>
<h4>From: {{a.feed|truncatechars:30}}</h4>
<p>
View Article
{% if user.is_authenticated %}
delete
{% endif %}
</p>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
{% block aside %}
<h3 class="panel-body panel panel-default text-center"></h3>
{% endblock aside %}
Find the template that is being rendered by whichever view is being called when '/blog/index/' is requested, and look in that template for {% url 'path_to_this_urls_file:feed_articles' %}. Also look in any templates which that template extends and/or includes, including template tags.
I added the appropriate markup but my videos are not showing up in Google search video tab.
Here is my markup:
{% block content %}
<div itemprop="video" itemscope itemtype="http://schema.org/VideoObject">
<meta itemprop="thumbnailUrl" content="{{post.image_url}}" />
<meta itemprop="embedURL" content="{{post.video_path}}" />
<meta itemprop="uploadDate" content="{{ post.publish }}" />
<h2 id="detail-h1">
<span itemprop="name">{{ post.title }}</span>
</h2>
{% if post.video %}
<div class="embed-responsive embed-responsive-16by9">
<iframe src="{{post.video_path}}?autoplay=1&autohide=1" class="embed-responsive-item"
frameborder="0" controls="0" allowfullscreen>
</iframe>
</div>
{% else %}
{% if post.image_url %}
<img src="{{post.image_url}}" class="img-responsive">
{% else %}
<p>upload an image</p>
{% endif %}
{% endif %}
<div style="margin-top: 10px; background: white; padding: 10px 10px 10px 10px;">
<span>
<p style="float: left">
{% if post.author %}
Author: {{post.author}}
{% endif %}
</p>
<p style="float: right" id="pub">
{%if post.draft %}<span id="draft">Draft </span>{% endif %} Publicado: {{ post.publish|date }}
</p>
</span>
<p class="tags" style="clear: both">Tags:
{% for tag in post.tags.all %}
<a href="{% url 'blog:post_list_by_tag' tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
{% if user.is_authenticated %}
<p>
delete
edit
</p>
{% endif %}
<p>Share on:
<a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}"
class="btn btn-social-icon btn-facebook" target="_blank">
<span class="fa fa-facebook"></span>
</a>
<a href="https://twitter.com/home?status={{ instance.content | truncatechars:80 | urlify }}%20{{ request.build_absolute_uri }}"
class="btn btn-social-icon btn-twitter" target="_blank">
<span class="fa fa-twitter"></span>
</a>
<a href='https://plus.google.com/share?url={{ request.build_absolute_uri }}'
class="btn btn-social-icon btn-google" target="_blank">
<span class="fa fa-google"></span>
</a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url={{ request.build_absolute_uri }}&title={{
instance.title }}&summary={{ share_string }}&source={{ request.build_absolute_uri }}"
class="btn btn-social-icon btn-linkedin" target="_blank">
<span class="fa fa-linkedin"></span>
</a>
</p>
</div>
<div style="margin-top: 10px; background: white; padding: 10px 10px 10px 10px;">
<p class="content-markdown"><span itemprop="description">{{post.body}}</span></p>
</div>
<div style="margin-top: 10px; background: white; padding: 10px 10px 10px 10px; margin-bottom: 10px">
<div id="disqus_thread"></div>
</div>
</div>
{% endblock content %}
{% block aside %}
<h3 class="">Similar Posts</h3>
<hr style="margin:10px">
<div class="row">
{% for post in similar_posts %}
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-12" id="similar">
<div class="thumbnail thumb">
<h6 id="date">{{ post.publish|date }}</h6>
{% if post.image %}
<img src="{{post.image.url}}" class="img-responsive post">
{% elif post.image_url %}
<img src="{{post.image_url}}" class="img-responsive post">
{% else %}
<p>upload an image</p>
{% endif %}
<div class="caption">
<a href="{{ post.get_absolute_url }}">
<h5 class="post-title" id="title">{{post.title}}</h5>
</a>
{% if user.is_authenticated %}
<p>
delete
edit
</p>
{% endif %}
</div>
</div>
</div>
{% empty %}
<p id="para">There are no similar posts yet.</p>
{% endfor %}
</div>
{% endblock aside %}
Today is Sunday May 8th 2016. I did this about 11:30 last night.
Does Google need to update its systems for it to show up? I ran it throught the Structured Data Testing Tool and It worked. I got a green check on my VideoObject. So Google is sensing it but it's not showing.
Another thing: My blog doesn't host videos. The videos are embeds from YouTube. When someone clicks the link on Google, will it send them to my site or to the source of the embed?