NoReverseMatch at display/success/ - python

In the get_success_URL method of FileView , i used 'display:redirect' as the URL to be redirected . But i am getting an error as
Reverse for 'redirect' with arguments '()' and keyword arguments '{'pk': 15}' not found. 1 pattern(s) tried: ['display/success/']
Request Method: POST
Request URL: http://127.0.0.1:8000/display/upload/
Django Version: 1.10.dev20160512164014
Exception Type: NoReverseMatch
Exception Value:
Am I missing something?
display/views.py
class FileView(FormView):
template_name = 'display/upload.html'
form_class = FileForm
def form_valid(self, form):
file_upload = FileModel(file=self.get_form_kwargs().get('files')['file'])
file_upload.save()
self.id = file_upload.id
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse('display:redirect', kwargs={'pk': self.id})
def redirect(request):
return render(request,"display/success.html")
display/urls.py
from django.conf.urls import url
from django.conf.urls.static import static
from django.conf import settings
from . import views
from display.views import FileView
urlpatterns = [
url(r'^start/', views.initial,name='home'),
url(r'^upload/',FileView.as_view(),name='upload'),
url(r'^success/',views.redirect,name='redirect'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Look at this line:
Reverse for 'redirect' with arguments '()' and keyword arguments '{'pk': 15}' not found.
The error message is telling you what's wrong. You've passed a keyword argument and when it tries to reverse / find the url it doesn't because you have the url specified as:
url(r'^success/',views.redirect,name='redirect')
Which doesn't accept any arguments. One possibility is to make your url into this:
^success/(?P<pk>\d+)/$

Related

Django, NoReverseMatch. What should I write in this code?

I have a problem:
NoReverseMatch at /update-orders/12
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update\\-orders/(?P<pk>[0-9]+)$']
Request Method: GET
Request URL: http://192.168.0.249:8000/update-orders/12
Django Version: 3.0.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update\\-orders/(?P<pk>[0-9]+)$']
Exception Location: /root/.local/share/virtualenvs/myp4-4l8n6HJk/lib/python3.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 677
Python Executable: /root/.local/share/virtualenvs/myp4-4l8n6HJk/bin/python
Python Version: 3.7.3
Python Path:
['/var/workspace/myp4/webprint',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/root/.local/share/virtualenvs/myp4-4l8n6HJk/lib/python3.7/site-packages']
Why do I have this Error during template rendering?
detail_order.html
{% extends 'index.html' %}
{% block content %}
<p>Back</p>
<h1>Заказ {{get_order.number_order}}</h1>
<h2>Update</h2>
views.py
class UpdateOrderView(CustomSuccessMessageMixin, UpdateView):
model = Order
template_name = 'detail_order.html'
form_class = OrderForm
success_url = reverse_lazy('detail_order')
success_msg = 'Ok'
def get_context_data(self, **kwargs):
kwargs['update'] = True
return super().get_context_data(**kwargs)
urls.py
from django.contrib import admin
from django.urls import path, include
from .views import *
from print import views
urlpatterns = [
path('', views.home_page, name='index'),
path('orders', views.OrderCreateView.as_view(), name='orders'),
path('update-orders/<int:pk>', views.UpdateOrderView.as_view(), name='update_order'), # THIS PATH
path('delete-orders/<int:pk>', views.DeleteOrderView.as_view(), name='delete_order'),
path('detail-order/<int:pk>', views.DetailOrderView.as_view(), name='detail_order'),
path('login', views.MyprojectLoginView.as_view(), name='login_page'),
path('register', views.RegisterUserView.as_view(), name='register_page'),
path('logout', views.MyprojectLogout.as_view(), name='logout_page'),
]
On your template, please put order.id instead of get_order.id. Then your template code will be :
<h2>Update</h2>

Reverse for 'about_abc' with arguments '('',)' not found

I am trying to set up web app. I am having issues with passing host_id to template .html file
i get:
Reverse for 'about_abc' with arguments '('',)' not found. 1 pattern(s) tried: ['itpassed\\/(?P<host_id>[0-9]+)\\/about\\/$']
inter.html
<li>about</li>
When i use "1" instead of "host_id" its working, but it can't stay hardcoded like this.
views.py
from django.shortcuts import render
import warnings
import requests
import json
from django.http import HttpResponse
from django.template import loader
from .models import Host
[...]
def inter(request, host_id):
return render(request, 'itpassed/inter.html')
def about_abc(request, host_id):
response = requests.get(
'abc.net:1768/abc/api/v1/about',
verify='/cert/cacerts.pem',
headers={'Accept': 'application/json', 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxx'},
)
return HttpResponse(response.content)
urls.py
from django.urls import path
from .models import Host
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:host_id>/', views.inter, name='inter'),
path('<int:host_id>/about/', views.about_abc, name='about_abc'),
]
How to fix this? As far as i can see views.py should pass host_id to template.
Why hardcoded "1" works but, host_id don't?
thanks
def inter(request, host_id):
return render(request, 'itpassed/inter.html')
You are not passing anything to the inter.html template here - so the host_id template variable won't hold any value.
I believe you want this instead:
def inter(request, host_id):
return render(request, 'itpassed/inter.html', {"host_id": host_id})

NoReverseMatch at /upload/ when redirecting

When I am executing the code,I get the following error
NoReverseMatch at /upload/
Reverse for 'compare' with keyword arguments '{u'uploaded_file_url2': '/media/SAMPLE1.xlsx', u'uploaded_file_url': '/media/SAMPLE.xlsx'}' not found. 1 pattern(s) tried: ['compare/']
myapp/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^upload/', views.upload,name='upload'),
url(r'^compare/(?P<uploaded_file_url>\w+)/(?P<uploaded_file_url2>\w+)$',views.compare,name='compare'),
]
views.py
from django.shortcuts import render,redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.conf.urls import url
from django.http import HttpResponseRedirect
from django.urls import reverse
def upload(request):
if request.method == 'POST':
myfile=request.FILES['myfile']
myfile2=request.FILES["myfile2"]
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
filename2=fs.save(myfile2.name, myfile2)
uploaded_file_url = fs.url(filename)
uploaded_file_url2 = fs.url(filename2)
return redirect(reverse('myapp:compare',kwargs={"uploaded_file_url":uploaded_file_url,"uploaded_file_url2":uploaded_file_url2 } ))
return render(request, 'myapp/upload.html')
def compare(request,uploaded_file_url,uploaded_file_url2):
a=uploaded_file_url
b=uploaded_file_url2
#some code here
return render(request,'myapp/compare.html',{"a":a,"b":b})
Here I want to pass uploaded_file_url and uploaded_file_url2 to compare
You're trying to pass all the data from the upload to the reverse function, so that it is part of the URL. But your "compare" URL does not expect any arguments at all.
Either change the URL so that it does expect arguments, or pass the uploaded filenames in the GET parameters or in the session.

NoReverseMatch Error on Django Redirect

I am getting the following exception in movie app of my django project:
Exception Type: NoReverseMatch at /movie/
Exception Value: Reverse for 'movie.views.movie_detail' with arguments '(u'the_peanuts_movie',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Here are the url patterns of movie.urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<movie_name>[\w_]+)$', views.movie_detail, name='movie_detail'),
url(r'^', views.movie_home, name='movie_home'),
]
Here are some parts of the movies.views.py:
def movie_detail(request, movie_name):
# code
...
def movie_home(request):
...
return redirect(movie_detail, movie_name)
# movie_name is a string
So far, I cannot make this code work. But it works, if I change return statement and put this in movie_home function:
movie_name += '/'
return redirect(movie_name)
Now after reading a few other questions of this type in stackoverflow, I know that the return redirect(movie_name) statement works since it redirects to an url:
127.0.0.1:8000/movie/whatever_is_the_movie_name
But I do not understand why redirecting to movie_detail view is not working.
How can I make it work?
First of all, movie_detail should be a string ('movie_detail') and under the 'movie' namespace - also, try using reverse and passing in the name as an arg:
return redirect(reverse('movie:movie_detail', args=[movie_name]))
Also, it looks like you missed an ending slash in your urls.py before the $:
url(r'^(?P<movie_name>[\w_]+)/$', views.movie_detail, name='movie_detail'),
Try This:
movie.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<movie_name>[\w_]+)$', views.movie_detail, name='movie_detail'),
url(r'^', views.movie_home, name='movie_home'),
]
movie.views.py
def movie_detail(request, movie_name):
# code
...
def movie_home(request):
...
return redirect('movie:movie_detail', movie_name) #CHANGES : (appname:url_name as str)
# movie_name is a string

Error during template rendering?

I have a blog app, this is the relevant part of views.py, of course belonging to the blog app.
from django.conf import settings
from django.shortcuts import render
from .models import Blog
# Create your views here.
def view_homepage(request):
return render(request, 'index.html', {})
def view_aboutpage(request):
return render(request, 'about.html', {})
def view_blogpost(request, blog_id):
article = Blog.objects.get(pk=blog_id)
return render(request, 'damn.html', {'article':article})
this is the urls.py in blog app
from django.conf.urls import url, include, patterns
from blog import views
urlpatterns = [
url(r'^$', views.view_homepage, name=''),
url(r'about/$', views.view_aboutpage, name='about'),
url(r'blog/(?P<blog_id>\d+)/$', views.view_blogpost, name='post'),
]
This is the regular urls.py in the project.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('blog.urls')),
]
This is the error, I am having.
NoReverseMatch at /
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<blog_id>\\d+)/$']
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.9
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<blog_id>\\d+)/$']
Exception Location: C:\Users\filip\Python\ENV\lib\site- packages\django\core\urlresolvers.py in _reverse_with_prefix, line 508
Python Executable: C:\Users\filip\Python\ENV\Scripts\python.exe
Python Version: 3.5.1
This is the html:
<li>
Home
</li>
<li>
About
</li>
<li>
Sample Post
</li>
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P\d+)/$']
Like the error says, you don't have a URL called "post" that takes no arguments; the URL you do have called that is expecting a blog_id argument. So, you should pass that in your tag; since you have a context variable called article it would be:
Sample Post

Categories