urls.py
from django.urls import path
from . import views
app_name = 'infos'
urlpatterns = [
path('', views.home_page, name='index_view'),
path('members/', views.list_members, name='list_members'),
path('members/<int:id>/', views.view_member, name='view_member'),
path('members/<int:id>/photos/', views.show_photos, name='show_photos'),
]
views.py
def show_photos(request, id):
member = BandMembers.objects.get(pk=id)
member_id = member.pk
print(member_id)
photos = Photos.objects.get(band_member=member)
return render(request, 'infos/photos.html', { 'photos': photos, 'member': member_id})
html template photos.html
{% extends 'base.html' %}
{% block title %}
Στοιχεία
{% endblock %}
{% block body %}
<div class="container">
{{ form.as_p }}
Φωτογραφίες
</div>
{% endblock %}
I get a NoReserveMatch error. What am i doing wrong? I've been searching for hours and hours at Stackoverflow, Youtube,... and it seems as if i'm doing it right. But surely I DON'T !!!!!!
Where is my mistake ??????? Thank you guys .
Related
I am trying to make a website that sorts equipment based on their category and i am getting the reverse match error when i link the urls on the webpage. Please i need advice on how i can properly pull this off. Thanks in advance.
urls.py
from django.urls import path
from . import views
app_name = 'equipments_catalogue'
urlpatterns = [
path('equipment_list/', views.EquipmentListView.as_view(), name='equipment_list'),
path('equipment_categories/', views.EquipmentCategoryView.as_view(),
name='equipment_categories'),
path('equipment_by_category/<str:cats>/', views.EquipmentListByCategory,
name='equipment_by_category')
]
views.py
from django.shortcuts import render
from django.views.generic import ListView
from . import models
# Create your views here.
class EquipmentCategoryView(ListView):
model = models.Category
template_name = 'equipment_categories.html'
def EquipmentListByCategory(request, cats):
equipment_category = models.Equipment.objects.filter(category=cats)
return render(request, 'equipment_by_category.html', {'cats': cats , 'equipment_category':
equipment_category})
class EquipmentListView(ListView):
model = models.Equipment
template_name = 'equipment_list.html'
template
{% extends 'base.html' %}
{% load static %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Welcome to JPI Equipment Categories Page</h1>
{% for category in object_list %}
<a href="{% url 'equipments_catalogue:equipment_categories' equipment_by_category.category
%}">{{ category.name }}</a><br><br>
{% endfor %}
{% endblock %}
You're calling it wrong. The correct way to call it is {% url '<app_name>:<url_name>' %}
Your app name is equipments_catalogue, your url name is equipment_by_category, not equipment_categories, which is correct, and the argument you should be passing should be a string, something like category.name:
{% url 'equipments_catalogue:equipment_by_category' category.name %}
I am very new at Django and I am working on a web app project. this particular page is supposed to edit an entry and save the entry. but I keep getting the missing 1 required argument
Views.py
# editPage forms
class editform(forms.Form):
content = forms.CharField(widget=forms.Textarea(), label='')
def edit(request, entry):
if request.method == 'GET':
page = util.get_entry(entry)
return render(request,"encyclopedia/edit.html",{
"form":SearchEntry(),
"edit":editform(initial={'content': page}),
"entry":entry
})
#If this is a POST request
else:
form = editform(request.POST)
if form.is_valid():
content = form.cleaned_data["content"]
util.save_entry(entry,content)
page = util.get_entry(entry)
page = mark.convert(page)
return render(request,"encyclopedia/entry.html",{
"form":SearchEntry(),
"page":page,
"entry": title
})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("newEntry", views.newEntry, name="newEntry"),
path("edit", views.edit, name="edit"),
edit HTML
{% extends "encyclopedia/layout.html" %}
{% block title %}
Edit {{name}}
{% endblock %}
{% block body %}
<h1>{{title}}</h1>
<form action= "{% url 'edit' %}" method="POST">
{% csrf_token %}
{{ edit }}
<br>
<input class="save btn btn-info" type="submit" value="save"/>
</form>
<p> Click the "save" button to save your entry to the encyclopedia.</p>
<br>
Return Home
{% endblock %}
entry HTML
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<h1>{{title}}</h1>
{{entry | safe}}
Edit Content
<br>
<br>
Return Home
{% endblock %}
when I change this particular url:
path("edit/<str:entry>", views.edit, name="edit"),
I get a different issue:
Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['edit/(?P[^/]+)$']
The problem is in your urls.py file in this line:
path("edit", views.edit, name="edit"),
because views.edit is expecting that you must provide two-parameter request and entry in your url. And in your case entry is missing. Try to add entry in your urlspatterns path, in this case, I'm expecting your entry is int.
path("edit/<int:entry>", views.edit, name="edit"),
and this entry can be your model pk or anything else you want. After modifying your urlspatterns whenever you call edit view in your html you need to do this:
{% url 'edit' entry=your_entry_value %}
instead of:
{% url 'edit' %}
So I'm making an episode tracker for tv shows, currently making the "Add show" form. When I go to localhost:8000/add, it says that that is not found. What could I possibly be doing wrong?
views.py
class ShowCreate(CreateView):
model = Show
fields = ['title', 'description', 'episode', 'season']
index.html
Add
urls.py
url(r'^add/$', views.ShowCreate.as_view(), name='show-add'),
show_form.html
{% extends 'show/base.html' %}
{% block title %}Add Show{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<h1>Add show</h1>
{% include 'show/form-template.html' %}
<button>Submit</button>
</form>
{% endblock %}
form_template.html
{% for field in form %}
<div class="form">
<h3>{{field.label_tag}}</h3>
<div class="field">{{field}}</div>
</div><!--form-->
{% endfor %}
This is everything in urlpatterns
urlpatterns = [
# index
url(r'^$', views.IndexView.as_view(), name='index'),
# show detail
url(r'^(?P<show>[\w.#+-]+)/$', views.ShowDetail.as_view(), name='show-detail'),
# form to add show
url(r'^add/$', views.ShowCreate.as_view(), name='show-add'),
# delete show
# signup
# login
# logout
]
Error message:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/add/
Raised by: show.views.ShowDetail
main urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('show.urls')),
]
I am working on extending the webapp we're left off with after completing the official Django Tutorial.
One of the functionalities I am looking to add is the ability for users to add polls themselves.
I am struggling with getting the page to process the data and then redirect to the index page ('/polls').
When I submit a new poll as a logged in user, I am returned to my index page, which is supposed to show most recently published polls, or in the event of no polls, the message "No polls are available."
For some reason, I always see "No polls are available", but once I click to the index page via a link on the site, it displays all of my polls, including my most recently created one, data intact!
Any thoughts here? I think I have included the relevant info below but happy to supply more. Thanks in advance for any help/advice.
views.py
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Poll.objects.order_by('-pub_date')[:15]
#login_required
def add_poll(request):
ChoiceFormSet = formset_factory(ChoiceForm, extra=3, min_num=2, validate_min=2)
if request.method == 'POST':
form = PollForm(request.POST)
formset = ChoiceFormSet(request.POST)
if all([form.is_valid(), formset.is_valid()]):
poll = form.save()
for inline_form in formset:
if inline_form.cleaned_data:
choice = inline_form.save(commit=False)
choice.question = poll
choice.save()
return render(request, 'polls/index.html', {})
else:
form = PollForm()
formset = ChoiceFormSet()
return render(request, 'polls/add_poll.html', {'form': form, 'formset': formset})
add_poll.html
{% extends 'polls/base.html' %}
{% block title %}Add Poll{% endblock %}
{% block body_block %}
<form role="form" id="poll_form" method="post" action="{% url 'polls:add_poll' %}">
<h2 class="form-signin-heading">Add a Poll</h2>
{% csrf_token %}
<table>
{{ form }}
{{ formset }}
</table>
<br/>
<button class="btn btn-primary" type="submit" name="submit">Create Poll</button>
</form>
{% endblock %}
index.html
{% extends 'polls/base.html' %}
{% block body_block %}
{% if latest_question_list %}
<ul>
{% for poll in latest_question_list %}
<li>{{ poll.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
{% endblock %}
urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^add_poll/$', views.add_poll, name='add_poll'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^profile_page/$', views.ProfileView.as_view(), name='profile_page'),
url(r'^edit_profile/$', views.edit_profile, name='edit_profile'),
)
When you save your form you are not redirecting.
Your are returning 'polls/index.html' with empty polls data, that's why you always get "No polls are available". But this is very incorrect, you must follow the Post/Redirect/Get (PRG) pattern, so instead of:
return render(request, 'polls/index.html', {})
do this:
return HttpResponseRedirect(reverse('polls:index'))
You don't do any redirect right now, you are just rendering your index template with an empty context (that's why you don't see anything). To redirect, you need to use HttpResponseRedirect when your form is valid.
So, please change line:
return render(request, 'polls/index.html', {})
(just over the else) to
return HttpResponseRedirect(reverse('index'))
I finally figured it out. I had to change my html form to this:
<form method="POST" action="{% url 'new_beam:beam_diagram' beam_id=1 %}" enctype="multipart/form-data">
Please help me. When I trying to access http://127.0.0.1:8000/myblog/ It display empty object list.
Here is my template file.
{% extends "base_entries.html" %}
{% block title %}{{block.super}} | Latest entries{% endblock %}
{% block content %}
{% for entry in object_list %}
<h2>{{entry.title}}</h2>
<p>Published on {{entry.pub_date|date:"F j, Y"}}</p>
{% if entry.excerpt_html %}
{{entry.excerpt|safe}}
{% else %}
{{entry.body_html|truncatewords_html:"50"|safe}}
{% endif %}
<p>Read Full entry</p>
{% endfor %}
{% endblock %}
this is my entries.py url. I have included into main urls.py
`from django.conf.urls.defaults import *
from myblog.models import Entry
entry_info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^$',
'archive_index',entry_info_dict,
'blog_entry_archive_index'),
)`
In archive_index generic view, the default name of the template variable is latest, not object_list.
archive_index documentation