Django: URL not recognized? Have no idea what I'm doing wrong - python

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')),
]

Related

How do I build a system to view a user's profile contents trough a link

I already have a profile viewing and updating page for user. But I want everyone able to view the users profile through a link in the user's post.
views.py:
def teacherprofile(request, id):
userT = User.objects.filter(id=id)
return render(request, 'viewprofile.html', {'posts': userT})
urls.py:
urlpatterns = [
path('', views.index, name='index'),
path('home', views.index, name='home'),
path('signup', views.signup, name='signup'),
path('postskill', views.postskill, name='postskill'),
path('profile', views.profile, name='profile'),
path('teacherprofile/<int:id>',views.teacherprofile, name='teacherprofile'),
path('post/<int:id>', views.post, name='post'),
path('post_delete/<int:pk>', views.post_delete, name='post_delete')
]
home.html template from where the url for teacherprofile is called:
{% for a in skills %}
<ul>
<div class = "rca">
<li>Posted By: <h5> {{a.teacher_name}} </h5></li>
<li>Subject Type: {{a.skill_type}}</li>
<li>Subject Name: {{a.name}}</li>
<li>Duration: {{a.duration}} months</li>
<li>Cost: Rs. {{a.cost}}</li>
<li>Location: {{a.location}}</li>
</div>
</ul>
{% endfor %}
viewprofile.html template:
{% extends "base_generic.html" %}
{% load crispy_forms_tags %}
{% block content %}
<img class="rounded-circle account-img" src="{{posts.image.url}}" height="100" width="100">
<h5> First Name: {{posts.first_name}} </h5>
<h5> Last Name: {{posts.last_name}} </h5>
<h5> Username: {{posts.username}} </h5>
<h5> Email: {{posts.email}} </h5>
<p> Bio: {{posts.profile.bio}} </p>
{% endblock %}
Note: there is no error log. when i click on someone's profile . the fields are blank.
enter image description here
in your views.py add .first() in your userT:
def teacherprofile(request, id):
userT = User.objects.filter(id=id).first()
.....
the filter is return QuerySet[]. the get_object_or_404 is return Instance, so you have to use .first() to get the user in QuerySet[], or use get_object_or_404.

ERROR 404 after submitting a form with POST request

http://127.0.0.1:8000/tasks works fine but when when I add a task and submit it, I get a 404 thrown at me,
ERROR: Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
polls/
newyear/
tasks/ [name='index']
tasks/ add [name='add']
The current path, tasks/{ % url 'add' % }, didn't match any
of these
mysite\tasks\urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path("", views.index, name='index'),
path("add", views.add, name='add')]
mysite\mysite\urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/',include('polls.urls')),
path('newyear/', include('newyear.urls')),
path('tasks/', include('tasks.urls'))]
mysite\tasks\views.py
from django.shortcuts import render
tasks = ["foo", "bar", "baz"]
def index(request):
return render(request, "tasks/index.html", {
"tasks": tasks
})
def add(request):
return render(request, "tasks/add.html")
)
mysite\tasks\templates\tasks\index.html
{% extends "tasks/layout.html" %}
{% block body %}
<h1>
Tasks
</h1>
<ul>
{%for task in tasks%}
<li> {{task}}</li>
{%endfor%}
</ul>
Add a New Task
{% endblock %}
mysite\tasks\templates\tasks\add.html
{% extends "tasks/layout.html" %}
{% block body %}
<h1>
Add Task
</h1>
<form action= "{ % url 'add' % }" method="post">
<input type="text" name="task">
<input type="submit">
</form>
View Tasks
{% endblock %}
Aside from Alexey Popov's answer to fix broken tags with space {% ... %}, also remember to add within your form a csrf token:
<form action= "{% url 'add' %}" method="post">
{% csrf_token %}
...

TypeError at /edit : edit() missing 1 required positional argument: 'entry'

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' %}

How to solve ReverseNoMatch error django?

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 .

Django-Registration redux password change forwards to index page

I can use the Django-Registration redux to login and register and it works fine except for the password change which forwards to: http://127.0.0.1:8000/?next=/accounts/password/change/ which lands on the homepage.
These are my app urls:
class MyRegistrationView(RegistrationView):
def get_success_url(self,user):
return('/createprofile/')
urlpatterns=[
url(r'^$', views.index, name='index'),
url(r'^city/(?P<city_name_slug>[\w\-]+)/$', views.show_city, name='show_city'),
url(r'^user/(?P<username>[\w\-]+)/$', views.show_profile, name='show_profile'),
url(r'^search/$', views.search, name='search'),
url(r'accounts/register/$', MyRegistrationView.as_view(), name='registraion_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^createprofile/$', views.createprofile, name="createprofile"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is the link the I use to change password:
<p>
forgot your password? Ney bother.
Reset password
</p>
This is the password_change_form.html form stored in the same place as other registration forms:
{% extends 'base.html' %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
This is the result from http://127.0.0.1:8000/accounts/:
EDIT: the above scenario happens when the user is not logged in. When the is logged in this error comes up:
NoReverseMatch at /accounts/password/change/ Reverse for
'auth_password_change_done' with arguments '()' and keyword arguments
'{}' not found. 0 pattern(s) tried: []
The issue turned out that I put this:
url(r'^accounts/', include('registration.backends.default.urls')),
in the app's url.py however, it needed to be in the project's url.py.

Categories

Resources