I working on pagination with ListView with django.
Everything is working as expected. But there is no next and previous links on template.
Am i missing something here ?
Here is urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic.list import ListView
from demoapp.models import Candidate
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^test/', ListView.as_view(
model=Candidate,
paginate_by='10',
queryset=Candidate.objects.all(),
context_object_name="tasks",
template_name='index.html')),
)
models.py
from django.db import models
class Candidate(models.Model):
name=models.CharField(max_length=255)
And here is index.html
<ol>
{% for t in tasks %}
<li><p>{{t.name}}</p></li>
{% endfor %}
</ol>
<div class="pagination">
<ul>
{% if paginator.has_previous %}
<li>Previous</li>
{% endif %}
{% for pg in paginator.page_range %}
{% if posts.number == pg %}
<li class="active">{{ pg }}</li>
{% else %}
<li>{{ pg }}</li>
{% endif %}
{% endfor %}
{% if paginator.has_next %}
<li>Next</li>
{% endif %}
</ul>
</div>
Nothing shows up because Django doesn't know what paginator is, in the view template.
Looking at the example in the documentation, it seems you need to replace paginator in the view with tasks -
<div class="pagination">
<ul>
{% if tasks.has_previous %}
<li>Previous</li>
{% endif %}
{% for pg in tasks.page_range %}
{% if posts.number == pg %}
<li class="active">{{ pg }}</li>
{% else %}
<li>{{ pg }}</li>
{% endif %}
{% endfor %}
{% if tasks.has_next %}
<li>Next</li>
{% endif %}
</ul>
</div>
Related
Reverse for 'redirection' with arguments '('',)' not found. 1 pattern(s) tried['Raccourcisseur/Lien/(?P[^/]+)/$']
[About errors][1]
#urls.py#
from django.urls import path
from . import views
urlpatterns = [
path('', views.liste,name='liste'),
path('nouveau/',views.afficher,name='afficher'),
path('Lien/<str:code>/',views.redirection,name='redirection'),
]
#urls.py(global)#
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('Raccourcisseur/',include('mini_url.urls')),
]
#views.py#
from django.shortcuts import render,redirect,get_object_or_404
from .models import MiniURL
from .forms import MiniURLForm
def afficher(request):
if request.method == "POST":
form = MiniURLForm(request.POST)
if form.is_valid():
form.save()
return redirect(liste)
else:
form = MiniURLForm()
return render(request,'mini_url/index.html',{'form':form})
def liste(request):
minis = MiniURL.objects.order_by('-acces')
return render(request, 'mini_url/liste.html', locals())
def redirection(request,code):
""" Redirection vers l'URL enregistrée """
mini = get_object_or_404(MiniURL, code=code)
mini.acces += 1
mini.save()
return redirect(mini.url, permanent=True)
#index.html#
{% extends 'mini_url/base.html' %}
{% block title %}
RaccourcisseurUrl
{% endblock%}
{% block body %}
<h1>Raccourcir une URL </h1>
<form method="post" action="{% url 'afficher' %}">
{% csrf_token %}
{{form.as_p}}
<input type="submit"/>
</form>
{% endblock %}
#liste.html#
{% extends 'mini_url/base.html' %}
{% block title %}
Page RaccourcisseurUrl
{% endblock%}
{% block body %}
<h1>Le raccourcisseur d'URL spécial </h1>
<p></p>Raccourcir une url </p>
<p>Liste des URLs raccourcies : </p>
<ul>
{% for mini in minis %}
<li>{{ mini.url }} via <a href="http://{{ request.get_host }}{% url 'redirection' mini.code %}">
{{ request.get_host } {% url 'redirection' mini.code %} </a>
{% if mini.pseudo %}
par {{ mini.pseudo }}
{% endif %} ({{mini.acces }} accès)
</li>
{% empty %}
<li> Il n'y en pas actuellement </li>
{% endfor %}
</ul>
{% endblock %}
In your list.html most certainly in {% url 'redirection' mini.code %} mini.code has no value.
I've been stuck on this for a while, can't seem to fix the error. I've checked the code a hundred times but obviously there is something I'm missing. I have installed my app also.
Can anybody see what I'm missing?
views.py
def survey_details(request, id=None):
context = {}
surveys = Survey.objects.get(id=id)
context['survey'] = survey
return render(request, 'surveydetails.html', context)
feedback.urls.py
path('details/<int:id>', views.survey_details, name="surveydetails"),
surveys.html
{% extends 'main.html' %}
{% block content%}
<h1>Surveys</h1>
<h2>list of {{title}} </h2>
{% if surveys %}
<ul>
{% for survey in surveys %}
<li>
{{ survey.title }}
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no surveys.</p>
{% endif %}
{% endblock %}
surveydetails.html
{% extends 'main.html' %}
{% block content%}
<h1>Surveys</h1>
<h2>Detail page</h2>
<h3>{{question.title}}</h3>
<p>Details page of {{question.title}}</p>
{% endblock %}
Here you are not passing the survey id.
{% for survey in surveys %}
<li>
{{ survey.title }}
</li>
{% endfor %}
I started to create login module in django. Login module is ok but I have problem with logout. When i click Logout - we see "error -ERR_TOO_MANY_REDIRECTS"
Probably something in this file is incorect: account/urls.py
from django.conf.urls import url
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'account'
urlpatterns = [
path('', auth_views.LoginView.as_view(template_name='account/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='registration/logout.html'), name='logout'),
path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
path('dashboard/', views.dashboard, name='dashboard'),
base.html
<body>
<div id="header">
{% if request.user.is_authenticated %}
<ul class="menu">
<li {% if section == "dashboard" %} class="selected"{% endif %}>
Panel główny
</li>
<li {% if section == "images" %} class="selected"{% endif %}>
Obrazy
</li>
<li {% if section == "people" %} class="selected"{% endif %}>
Ludzie
</li>
</ul>
{% endif %}
<span class="user">
{% if request.user.is_authenticated %}
Witaj, {{ request.user.first_name }}
Wyloguj
{% else %}
Zaloguj
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
logout.html
{% extends "base.html" %}
{% block title %} Wylogowanie {% endblock %}
{% block content %}
<h1>Wylogowanie</h1>
<p>Zostales wylogowany. Mozesz
zalogowac sie ponownie</p>
{% endblock %}
settings.html
...
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_REDIRECT_URL = reverse_lazy('account:logout')
show error
You've set LOGOUT_REDIRECT_URL to point back to the LogoutView which will cause a redirect loop. The LOGOUT_REDIRECT_URL should point to a URL that the user will be redirected to after they've logged out using the LogoutView.
Setting LOGOUT_REDIRECT_URL will override any template that's been set. Since you've explicitly set a template for the LogoutView in your urls.py, you should remove LOGOUT_REDIRECT_URL from your settings which will allow the template to be rendered.
When I click on my delete project link it takes me to my delete page with a button to click which should delete the model data of the project and then take me to the profile page. However when I click the delete button, the page just refreshes and no data gets deleted?!
What am I doing wrong here? Any help would be much appreciated :-)
Views
class DeleteProject(UpdateView):
model = UserProject
template_name = 'howdidu/delete_project.html'
def get_object(self, queryset=None):
obj = super(DeleteProject, self).get_object()
if not obj.user == self.request.user:
raise Http404
return obj
def get_success_url(self):
project_username = self.request.user.username
#project_slug = self.object.slug
return reverse('user_profile', kwargs={'username':project_username})
delete_project.html template
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}Delete project{% endblock %}
{% block body_block %}
<h1>Delete project</h1>
<form method="post">{% csrf_token %}
<p>Are you sure you want to delete "{{ userproject.title }}"?</p>
<input type="submit" value="Confirm" />
</form>
{% endblock %}
Urls
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/update_project/$', login_required(views.UpdateProject.as_view()), name='update_project'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/delete_project/$', login_required(views.DeleteProject.as_view()), name='delete_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
Project.html template which has the delete link on
{% extends 'howdidu/base.html' %}
{% load staticfiles %}
{% block title %}Project{% endblock %}
{% block body_block %}
{% if project %}
<h1>{{ project.title }}</h1>
<img src="{{ project.project_picture.url }}" width = "300" height = "300" />
<h3>{{ project.project_overview }}</h3>
{% if user.is_authenticated %}
{% if project_user.username == user.username %}
<p>Edit project</p>
<p>Delete project</p>
{% endif %}
{% endif %}
{% else %}
The specified project {{ project.title }} does not exist!
{% endif %}
{% endblock %}
You must use DeleteView not UpdateView.
See here.
On DetailView template I am not able to print name,date of post only '|' sign is being printed but on ListView it is working fine. Here is code written in files. Thanks!!
blog/urls.py
urlpatterns = patterns('',url(r'^$', ListView.as_view(queryset =
Blog.objects.all(), template_name ='blog.html')),
url(r'^(?P<pk>\d+)/$', DetailView.as_view(model=Blog, template_name='detail.html')), )
blog.html
{% extends 'base.html' %}
{% block content %}
{% for post in object_list %}
<ul> {{ post.name }} ||{{ post.date }}</ul>
{% endfor %}
{% endblock %}
detial.html
{% extends 'base.html' %}
{% block content %} <h2> <a href="/blog/{{ post.id }}">{{ post.name }}
</a>||{{ post.date }}</h2> {% endblock %}
context_object_name Designates the name of the variable to use in the context. Default is "object"
Try
{% extends 'base.html' %}
{% block content %} <h2> <a href="/blog/{{ object.id }}">{{ object.name }}
</a>||{{ object.date }}</h2> {% endblock %}
See SingleObjectMixin and making-friendly-template-contexts