YearArchiveView.date_field is required. ImproperlyConfigured - django

I open up the documentation to Django.
I try to use the (YearArchiveView) view. when I go through the URL index/2020/ I get this error( YearArchiveView.date_field is required. ) what I understand that I have to use the attribute that called date_field and I already use.
so, how can I fix that error?
also, I need to know how can I access on this URL by any link into another page?, like from index.html
views.py
from django.shortcuts import get_object_or_404, Http404, render_to_response
from django.views.generic import ListView, DetailView
from .models import Article, User
from .forms import ContactForm
from django.views.generic.edit import FormView
from django.urls import reverse_lazy
from django.views.generic.dates import ArchiveIndexView, YearArchiveView
class ArticleYearArchiveView(YearArchiveView):
queryset = Article.objects.all()
template_name = 'index/article_archive_year.html'
date_field = "date"
allow_future = True
urls.py
from . import views
from django.urls import path
urlpatterns = [
path("<int:year>/", views.YearArchiveView.as_view(), name="article_archive_year")
]
artcile_archive_year.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Archive year</title>
</head>
<body>
{% for date in date_list %}
<li>{{ date|date }}</li>
{% endfor %}
</body>
</html>

You are not correctly referencing your view object.
Instead of:
urlpatterns = [path("<int:year>/", views.YearArchiveView.as_view(), name="article_archive_year")]
Try:
urlpatterns = [path("<int:year>/", views.ArticleYearArchiveView.as_view(), name="article_archive_year")]
Specifically, note the use of views.ArticleYearArchiveView instead of views.YearArchiveView.

Related

why my added layers are not displayed by leaflet.ajax.js

I am developing a web map app using django, leaflet and postgis. My web app could display the OSM (openstreetmap) as background map but my added layer (from QGIS) and point data model (constructed in geodjango) could not be displayed in the web app. please give me a help. [Ofcourse I can see the text format (as json) in my added layer as follows:
https://i.stack.imgur.com/qYnWe.jpg
The code of index.html:
<!DOCTYPE html>
<html>
{% load static %}
{% load leaflet_tags %}
<head>
{% leaflet_js %}
{% leaflet_css %}
<title>Service Maps for Tourism</title>
<style type="text/css">
#gis {width: 80%; height: 800px;}
</style>
<script type="text/javascript" src= "{% static 'dist/leaflet.ajax.js' %}"> </script>
</head>
<body>
<h1> سامانه نقشه گردشگری</h1>
<br/>
<script type="text/javascript">
function our_layers(map,options){
var datasets = new L.GeoJSON.AJAX("{% url 'name' %}",{
});
datasets.addTo(map);
}
</script>
{% leaflet_map "gis" callback="window.our_Layers" %}
</body>
</html>
The views.py is as follows:
from django.shortcuts import render
from django.views.generic import TemplateView
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import Provinces
# Create your views here.
class HomePageView(TemplateView):
template_name= 'index.html'
def province_dataSets (request):
provinces= serialize('geojson', Provinces.objects.all())
return HttpResponse(provinces, content_type= 'json')
The models.py is as follows:
from __future__ import unicode_literals
from django.db import models
from django.contrib.gis.db import models
from django.db.models import Manager
# Create your models here.
class TourismPoints(models.Model):
name= models.CharField(max_length= 50)
location= models.PointField(srid= 4326)
objects= models.Manager()
def __unicode__(self):
return self.name
class Meta:
verbose_name_plural= "Tourism points"
class Provinces(models.Model):
name = models.CharField(max_length=50)
english_name = models.CharField(max_length=40)
geom = models.MultiPolygonField(srid=4326)
def __unicode__(self):
return self.name
class Meta:
verbose_name_plural= "Provinces"
The urls.py is as follows:
from django.conf.urls import include, url
from .views import HomePageView, province_dataSets
from django.urls import path
urlpatterns = [
path ('', HomePageView.as_view (), name='home'),
path('province_data/', province_dataSets, name='name'),]
I used version 2.7 of Django and fortunately the added layers could be displayed

django updateview not showing existing data to form

update is working but template form is not showing existing data. django updateview not showing existing data to form. when update page is showing only existing file is showing but object data is not showing. updateview cannot send existing data to form
view.py
from django.shortcuts import render, redirect
from django.core.files.storage import FileSystemStorage
from .forms import BookForm
from .models import Book
from django.views.generic import TemplateView, ListView, CreateView, UpdateView
from django.urls import reverse_lazy
class BookUpdate(UpdateView):
model = Book
form_class = BookForm
success_url = reverse_lazy('class_book_list')
template_name = 'updatebook.html'
model.py
from django.db import models
from django.urls import reverse
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
pdf = models.FileField(upload_to='books/pdfs/')
cover = models.ImageField(upload_to='books/covers/', null=True, blank=True)
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ('title', 'author', 'pdf', 'cover')
urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('books/', views.book_list, name='book_list'),
path('books/upload/', views.upload_book, name='upload_book'),
path('books/<int:pk>/',views.delete_book, name='delete_book'),
path('class/books/',views.BookListView.as_view(), name='class_book_list'),
path('class/books/upload/',views.UploadBookView.as_view(), name='class_upload_book'),
path('class/books/update/<int:pk>/',views.BookUpdate.as_view(), name='class_update_book'),'''
updatebook.html
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2> Upload Book to Database</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form | crispy}}
<button type="submit" >Update</button>
</form>
</body>
</html>
html form cannot display existing data from database
please help
Make your method to GET i.e. (method="GET") in the form from where you are requesting the UpdateView page.

NoReverseMatch at / in django

Hi Everyone i want to go on detail view but i get this error while paste the url
NoReverseMatch at /
Reverse for 'test' with no arguments not found. 1 pattern(s) tried: ['test/(?P<pk>[0-9]+)/$']
Here is my Models.py
from django.db import models
# Create your models here.
class article(models.Model):
Title = models.CharField(max_length=200)
Content = models.CharField(max_length=1000)
Here is my Views.py
from django.shortcuts import render
from django.views.generic import (ListView,DetailView)
from .models import *
# Create your views here.
class Article(ListView):
model = article
class Test(DetailView):
model = article
Here is my Urls.py
from django.contrib import admin
from django.urls import path
from cbs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.Article.as_view(),name="Article"),
path('test/<int:pk>/', views.Test.as_view(),name="test"),
]
Here is my Article_list.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
{% for article in article_list %}
<h1>{{ article.Title }}</h1>
{% endfor %}
</body>
</html>
in your Article_list.html file you must change the href attribute on a tag to:
<h1>{{ article.Title }}</h1>
to send id as parameter to the below url:
path('test/<int:pk>/', views.Test.as_view(),name="test"),

Image is not showing in django template but other tag is working

I am trying to show up my image in django template. But the image is not showing. There is a headline tag which is working fine. I don't know where I am stuck. I searched it here and got a lot of solution, but none of this are not solving my issue.
Here is my views.py:
from django.shortcuts import render
from .models import Post
from django.utils import timezone
# Create your views here.
def home(request):
posts = Post.objects.order_by('id')
return render(request, 'home.html')
Here is my urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from Picture import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And this is home.html:
{% load static %}
<html>
<head>
<title>Home Page</title>
<meta charset='utf-8'>
</head>
<body>
<h1>{{ posts.title }}</h1>
<img src="{{ posts.image.url }}" alt="image-{{ post.title }}">
</body>
Images are in the media directory. Please help me to fix my problem.
I see two problems with your code
return render(request, 'home.html') does not have posts context parameter
In your template file posts object is likely to be a queryset, you need to loop through it to render its properties
def home(request):
posts = Post.objects.order_by('id')
return render(request, 'home.html', {"posts":posts})

CSRF token missing or incorrect explanation

i am just a beginner and starting with some tutorials on web, i can not understand why this isn't working for me:
my views.py
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.core.context_processors import csrf
class MainPage(View):
def get(self, request):
return render_to_response("helloworld.html")
class TestHandler(View):
def post(self, request):
q = {}
q.update(csrf(request))
#return render_to_response('test.html', q)
return render_to_response('test.html', {'q':q}, context_instance=RequestContext(self.request))
def get(self, request):
q = self.request.GET.get('q')
return HttpResponse(q)
and my urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from views import MainPage, TestHandler
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^hello$', MainPage.as_view(), name='home'),
url(r'^testform/', TestHandler.as_view()),
url(r'^admin/', include(admin.site.urls)),
helloworld.html
>->-<html>
>->->-<head>
>->->->-<title>Hello, World!</title>
>->->-</head>
>->->-<body>
>->->->-<form method="post" action="/testform/" >
{% csrf_token %}
>->->-<input name="q">
<input type="submit">
</form>
>->->-</body>
>->-</html>
test.html
>-<body>
>-Hello {{q}}
>-</body>
This is running on django 1.6, I read most of post and still can't figure it out.
Unfortunately what you have pasted is a bit of a mess, you are using Class Based Views but yet you have mixed them with function based views (also half of the declarations are missing).
Enable the CSRF Middlware in your settings.py
MIDDLEWARE_CLASSES = (
...
'django.middleware.csrf.CsrfViewMiddleware',
...
)
Fix your views to proper Class Based Views, what you have pasted is totally wrong:
from django.views.generic import CreateView, TemplateView
from django.core.urlresolvers import reverse_lazy
# Create the form in your forms.py
from .forms import (
MyTestForm,
)
class MainPage(TemplateView):
template_name = "test.html"
class TestHandler(CreateView):
form_class = MyTestForm
template_name = "helloworld.html"
success_url = reverse_lazy('home')
Create your form template:
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<form method="post" action="/testform/">
{% csrf_token %}
<input name="q">
<input type="submit">
</form>
</body>
</html>