I'm trying to make a query which orders the objects and then returns the first 100.
Orders.objects.order_by('date', 'time')[:100]
However I get this error.
Cannot reorder a query once a slice has been taken.
How would I make this query?
Edit:
Context
from django.shortcuts import render
from django_tables2 import RequestConfig
from .models import Order
from .tables import OrderTable
def order(request):
table = OrdersTable(Orders.objects.order_by('date', 'time')[:100])
RequestConfig(request).configure(table)
return render(request, 'view_orders/index.html', {'table': table})
# app/tables.py
import django_tables2 as tables
from .models import Order
class OrderTable(tables.Table):
class Meta:
model = Order
attrs = {'class': 'table', 'id': 'Order'}
Django-tables2 is giving this error, it needs to call order on the query however you are passing a already sliced list instead. A work around is explained in this reported issue. You can also use the render portion only as in example provided in doc:
from django.shortcuts import render
def people(request):
return render(request, 'people.html', {'people': Person.objects.all()})
for the template:
{# tutorial/templates/people.html #}
{% load render_table from django_tables2 %}
{% load static %}
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css' %}" />
</head>
<body>
{% render_table people %}
</body>
</html>
Related
How is going? I'm learning to programming in django. For the moment I'm building a simple app that utilizing a form update the referenced table.
Now I'm try to add a delete button in each row of my table but, beside I have tried a lot of solutions, I didn't find one that works correctly.
Below my code:
urls
from django.urls import path
from app import views
app_name = 'main'
urlpatterns = [
path('', views.homepage, name='homepage'),
path('delete_item/<int:pk>', views.delete_item, name="delete_item"),
]
forms
from django import forms
from .models import Income
class IncomeModelForm(forms.ModelForm):
class Meta:
model = Income
fields = "__all__"
tables
import django_tables2 as tables
from django_tables2.utils import A
from .models import Income
class PersonTable(tables.Table):
delete = tables.LinkColumn('main:delete_item', args=[A('delete-id')], attrs={'a': {'class': 'btn'}})
class Meta:
model = Income
template_name = "django_tables2/bootstrap.html"
views
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView
from .models import Income
from .tables import PersonTable
from .forms import IncomeModelForm
def homepage(request):
table = PersonTable(Income.objects.all())
if request.method == 'POST':
form = IncomeModelForm(request.POST)
if form.is_valid():
print("Il form è valido")
new_input = form.save()
else :
form = IncomeModelForm()
context= {"form": form,
"table":table }
return render(request, "app/base.html", context)
def delete_item(request, pk):
Income.objects.filter(id=pk).delete()
items = Income.objects.all()
context = {
'items': items
}
return render(request, 'app/base.html', context)
html
{% load static %}
{% load render_table from django_tables2 %}
<!doctype html>
<html lang="it">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Hello, world!</title>
</head>
<div class="container">
<form class="" action="" method="post">
{% csrf_token %}
{{form|crispy}}
<input type="submit" class="btn btn-danger" value="INVIA">
</form>
</div>
<br>
<br>
<div class="container">
{% render_table table %}
</form>
</div>
</body>
</html>
My table disply the column "Delete" but no buttoms, only a "-". Why? Where is my error?
Please add this
text='static text', argument like delete = tables.LinkColumn('main:delete_item',text='Delete', args=[A('delete-id')], attrs={'a': {'class': 'btn'}})
.
Hope it will work
Just quick info I'm a beginner in python and django.
Here is an issue:
I'm trying to create simple blog app with django, I've created all whats needed and I'm able to render one of my main pages however when I try to access posts page(to be able to view current posts) I receive the following error:
Request Method: GET
Request URL: http://localhost:8000/posts/
Django Version: 2.0.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post' not found. 'post' is not a valid view function or pattern name.
I'm not sure why I receive this error as I have posts view function written and accessible, here are my files:
my_blog/urls.py
"""Defines URL patterns for my_blog."""
from django.conf.urls import url
from . import views
#needed app name
app_name='my_blog'
urlpatterns=[
#Homepage
url(r'^$',views.home,name='home'),
# Show all posts.
url(r'^posts/$', views.posts, name='posts'),
models.py
from django.db import models
# Create your models here.
class BlogPost(models.Model):
"""Post that can be viewed"""
title=models.CharField(max_length=200)
text=models.TextField()
date_added=models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model."""
if len(self.text)<50:
return self.text
else:
return self.text[:50] + "..."
views.py
from django.shortcuts import render
from .models import BlogPost
from django.http import HttpResponseRedirect
# Create your views here.
def home(request):
"""The home page for Learning Log"""
return render(request,'my_blog/home.html')
def posts (request):
"""Show all posts."""
posts=BlogPost.objects.order_by('date_added')
context={'posts':posts}
return render(request, 'my_blog/posts.html', context)
HTML pages:
home.html
{%extends "my_blog/base.html"%}
{%block content%}
<html>
<p align="center">Welcome to my Blog</p>
</body>
</html>
{%endblock content%}
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Blog</title>
</head>
<body bgcolor="#85C1E9">
<p>
Home -
Posts
</p>
{%block content%}{%endblock content%}
</body>
</html>
posts.html
{%extends "my_blog/base.html"%}
{%block content%}
<p>Posts</p>
<ul>
{%for post in posts%}
<li>{{post}} </li>
{%empty%}
<li>No posts have been added yet.</li>
{%endfor%}
</ul>
{%endblock content%}
Thanks in advance for assistance
In your template you have:
{% url 'my_blog:post' post.id %}
This gives an error because you have not yet defined a URL pattern in my_blog/urls.py with name="post".
The view name that you are looking up in your template does not match the name defined in your urls.py. You need to make
urls.py
url(r'^posts/$', views.posts, name='posts'),
and
posts.html
<li>{{post}} </li>
match by either adding an s in posts,html or remove the s in urls.py
Edit
Looking a little deeper... You need to define your detail view (post with a parameter) in views.py. And add a corresponding entry in urls.py Then undo the change I recommended earlier.
You should also consider renaming your views from posts and post to something like post_list and post_detail
I have followed the official documentation of django tables 2 but it is not working I dont know why
views.py
from django.shortcuts import render
from django_tables2 import RequestConfig
from django.template.response import TemplateResponse
from .models import Customer
from .tables import CustomerTable
from .tables import TableView
from django_tables2.export.export import TableExport
def user_profile(request):
table= CustomerTable(Customer.objects.all())
RequestConfig(request,paginate={'per_page':15}).configure(table)
return render(request, 'home.html', {'table': table})
def TableView(request):
table = CustomerTable(Customer.objects.all())
RequestConfig(request).configure(table)
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, table)
return exporter.response('table.{}'.format(export_format))
return render(request, 'table.html', {
'table': table
})
my html template
{% extends 'base.html' %}
{% block content %}
{% load render_table from django_tables2 %}
{% load querystring from django_tables2 %}
<!doctype html>
<html>
<head>
<title>List of Customers</title>
</head>
<body>
{% querystring '_export'='csv' %}home
{% for format in table.export_formats %}
<a href="{% querystring '_export'=format %}">
download <code>.{{ format }}</code>
</a>
{% endfor %}
<p margin-bottom:500px;> </p>
<div style="width: 2500px; height: 600px; overflow-y: scroll;">
<div id ="users">
<input class="search" placeholder="Search" />
{% render_table table %}
{% endblock %}
</div>
</div>
</body>
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', user_profile, name="user_profile"),
url(r'^$', TableView, name="TableView"),
]
tables.py
import django_tables2 as tables
from .models import Customer
from django_tables2.export.views import ExportMixin
from .models import Customer
class CustomerTable(tables.Table):
class Meta:
model = Customer
template = 'django_tables2/bootstrap.html'
class TableView(ExportMixin, tables.SingleTableView):
table_class = CustomerTable
model = Customer
template_name = 'django_tables2/bootstrap.html'
I have a feeling something is wrong with the way I have defined urls.py but i cannot figure out what is wrong
There is a note in the documentation on exporting saying:
This example assumes you define a list of possible export formats on your table instance in attribute export_formats.
So you need to add the desired export formats to a list on your table like this:
class CustomerTable(tables.Table):
export_formats = ['csv', 'xls']
class Meta:
model = Customer
template = 'django_tables2/bootstrap.html'
You an also define the list in a context variable for your template, if you prefer that. There is no 'right' in this.
So I also banged my head against the wall a little bit on this, and then in a moment of clarity it hit me, you need wrap the query string tag like this in the template file
download as csv
I'm trying to set up a form on Django that displays inputs on the page, but I get this error.
django.db.utils.OperationalError: no such table: firstapp_post
This doesn't happen right away, but when I try to use the submit feature on my form.
Right now this is what I have as my models:
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
post = models.CharField(max_length=500)
user = models.ForeignKey(User)
These are currently my forms:
from django import forms
from firstapp.models import Post
class IndexForm(forms.ModelForm):
post = forms.CharField()
class Meta:
model = Post
fields = ('post',)
This is my views file:
from django.shortcuts import render, redirect
from firstapp.forms import IndexForm
from django.views.generic import TemplateView
class HomePage(TemplateView):
template_name = 'home/home.html'
def get(self, request):
form = IndexForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = IndexForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = IndexForm()
return redirect('home:home')
args = {'form': form, 'text': text}
return render(request, self.template_name, args)
This is my base.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Assignment 4</title>
<link rel='stylesheet' href='{% static "css/base.css" %}'/>
</head>
<body>
<p>{{ variable }}</p>
{% block body %}{% endblock %}
<script src= '{% static "js/base.js" %}'></script>
</body>
</html>
and my home.html:
{% extends 'base.html' %}
{% block body %}
<div class="container">
<p>Home</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
<p>{{ text }}</p>
</div>
{% endblock %}
Does anyone have any idea what this error even means or why I'm getting it? This has been driving me nuts. Thanks for the help!
As the error message mentions, that particular table does not exist in your database.
You can run the following command:
python manage.py makemigrations appname
By running makemigrations, you’re telling Django that you’ve made some changes to your models and that you’d like the changes to be stored as a migration.
Now run migrate again to create those model tables in your database
python manage.py migrate
Further Reading
I'm trying to display a simple form input-text box with Django. I'm am deploying on Amazon AWS. The site works fine on a different server (pythonanywhere) but there is a major problem on AWS. Specifically, the input box is not being displayed. I'm using templates as follows:
home.html
{% extends 'lists/base.html' %}
{% block header_text %}Start a new To-Do list {% endblock %}
{% block form_action %}{% url 'new_list' %}{% endblock %}
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>To-Do lists</title>
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/base.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 jumbotron">
<div class="text-center">
<h1>{% block header_text %}{% endblock %}</h1>
<form method="POST" action="{% block form_action %}{% endblock %}">
{{ form.text }}
{% csrf_token %}
{% if form.errors %}
<div class = "form-group has-error">
<span class = "help-block">{{ form.text.errors }}</span>
</div>
{% endif %}
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
{% block table %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
models.py
from django.db import models
from django
.core.urlresolvers import reverse
class List(models.Model):
def get_absolute_url(self):
return reverse('view_list', args=[self.id])
# Create your models here.
class Item(models.Model):
text = models.TextField(default = '')
list = models.ForeignKey(List, default = None)
#list = models.ForeignKey(List , default=None)
forms.py
from django import forms
from lists.models import Item
EMPTY_ITEM_ERROR = "You can't have an empty list item"
class ItemForm(forms.models.ModelForm):
class Meta:
model = Item
fields = ('text',)
widgets ={
'text' : forms.fields.TextInput(attrs={
'placeholder': 'Enter a to-do item',
'class': 'form-control input-lg',
}),
}
error_messages = {
'text' : { 'required': EMPTY_ITEM_ERROR }
}
views.py
from django.shortcuts import redirect, render
from lists.models import Item, List
from django.core.exceptions import ValidationError
from lists.forms import ItemForm
from lists.models import Item, List
# Create your views here.
def home_page(request):
return render(request, 'lists/home.html', {'form': ItemForm()})
urls.py
from django.conf.urls import url
from lists import views
urlpatterns = [
url(r'^new$', views.new_list, name='new_list'),
url(r'^(\d+)/$', views.view_list, name='view_list'),
]
Currently the site displays the following:
However it should (and does on a different website) display this:
I've pushed/pulled the entire project to github and the code between each site is identical, yet I'm not seeing why the text input isn't displayed, unless the form needs to be initialized in Django somehow or a quirk to AWS?
When comparing the two sites, the one without the text-box does not generate the following:
<input class="form-control input-lg" id="id_text" name="text" placeholder="Enter a to-do item" type="text" />
Even though it should, per the base.html syntax.
Updated
The full views.py (per suggested comment) is:
from django.shortcuts import redirect, render
from lists.models import Item, List
from django.core.exceptions import ValidationError
from lists.forms import ItemForm
from lists.models import Item, List
# Create your views here.
def home_page(request):
return render(request, 'lists/home.html', {'form': ItemForm()})
def new_list(request):
form = ItemForm(data=request.POST)
if form.is_valid():
list_ = List.objects.create()
Item.objects.create(text=request.POST['text'], list=list_)
return redirect(list_)
else:
return render(request, 'lists/home.html', {"form": form})
def view_list(request, list_id):
list_ = List.objects.get(id=list_id)
form = ItemForm()
if request.method == 'POST':
form = ItemForm(data=request.POST)
if form.is_valid():
Item.objects.create(text=request.POST['text'], list=list_)
return redirect(list_)
return render(request, 'lists/list.html', {'list': list_, "form": form})
In my experience with Django, there are 2 things you often (always?) need to do to get static files to "refresh" after pushing them to a remote server:
Run ./manage.py collectstatic to make sure all your static files are in the right place.
While sshed into your server run the command sudo reboot now to restart your server (note that this will kick you out of your ssh session, and your server will be unreachable for a moment - usually just a few seconds in my case).
As for step 2 there might be a better way to do this, but in my experience, when I update static files the updated version is not served until I do this, and restarting nginx or the like is not sufficient for the changes to take effect. Note that this will mean that your site, if live, will not be reachable for a few seconds while the server is restarting (which is what makes me think there might be a better way to do it) but for me and my small user base this is not a big issue.
From reading some other posts about static files not updating, it seems like it could also be the case that your browser is caching the static files, and that restarting your browser/clearing the cache might do the trick as well, but I have not had a chance to try this yet.