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.
Related
ValueError at /
The view leads.views.home_page didn't return an HttpResponse object. It returned None instead.
this is my code:
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home_page(request):
# return HttpResponse('Hello world')
render(request, 'leads/home_page.html')
and here's the urls.py:
from django.contrib import admin
from django.urls import path
from leads.views import home_page
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_page),
]
and here's the template:
The issue is here
def home_page(request):
render(request, 'leads/home_page.html')
render combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. So you have to return this HttpResponse back from view.
def home_page(request):
return render(request, 'leads/home_page.html')
After filling the page should appear 'done', but i have an error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/candidate/done.html
The current path, candidate/done.html, didn't match any of these.
Can't configure redirect to 'done' page.
Here views.py:
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import AnketaForm
from .models import Anketa
def anketa_create_view(request):
if request.method == 'POST':
form = AnketaForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('candidate/done.html')
else:
form = AnketaForm()
return render(request, 'candidate/anketa_create.html', {'form': form})
urls.py (apps/candidate)
from django.urls import path
from . import views
urlpatterns = [
path('', views.anketa_create_view, name = 'anketa_create_view'),
]
urls.py
from django.contrib import admin
from django.urls import path, include
from candidate.views import anketa_create_view
urlpatterns = [
path('done/', anketa_create_view),
path('', anketa_create_view),
path('grappelli/', include('grappelli.urls')),
path('admin/', admin.site.urls),
]
you need to give a name to the done url
path('done/', anketa_create_view, name='done'),
and can do with reverse
return HttpResponseRedirect(reverse('done'))
or you can do redirect shortcut
return redirect('done')
Remove in urls.py
path('done/', anketa_create_view)
and replace in views.py
return HttpResponseRedirect('candidate/done.html')
to
return render(request, 'candidate/done.html', {})
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})
I am trying to define a class in views.py which is inheriting generic view. The code is as follows. On running server I get the error that
class UserFormView(View):
NameError: name 'View' is not defined
although I have imported generic. Please let me know the reason.
from django.views import generic
from django.utils import timezone
from django.shortcuts import render, get_object_or_404,render_to_response,redirect
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth import authenticate,login
from django.core.context_processors import csrf
from .forms import UserForm
# Create your views here.
def home(request):
return render(request, 'fosssite/home.html')
def login(request):
c={}
c.update(csrf(request))
return render_to_response('fosssite/login.html',c)
class UserFormView(View):
form_class=UserForm
template_name='fosssite/signup.html'
def get(self,request):
form=self.form_class(None)
return render(request,self.template_name,{'form':form})
#validate by forms of django
def post(self,request):
form=self.form_class(request.POST)
if form.is_valid():
# not saving to database only creating object
user=form.save(commit=False)
#normalized data
username=form.cleaned_data['username']
password=form.cleaned_data['password']
#not as plain data
user.set_password(password)
user.save() #saved to database
def auth_view(request):
username=request.POST.get('username', '')
password=request.POST.get('password', '')
user=auth.authenticate(username=username,password=password)
if user is not None:
auth.login(request,user)
return HttpResponseRedirect('/loggedin')#url in brackets
else:
return HttpResponseRedirect('/invalid')
def loggedin(request):
return render_to_response('fosssite/loggedin.html',{'fullname':request.user.username})
def logout(request):
auth.logout(request)
return render_to_response('fosssite/logout.html')
def invalid_login(request):
return render_to_response('fosssite/invalid_login.html')
`
You need to either import View explicitly:
from django.views.generic import View
or refer to it as generic.View:
class UserFormView(generic.View):
# ...
The View name needs to be imported. Add the following import statement:
from django.views.generic import View
Or use the already imported generic module in
class UserFormView(generic.View)
# ^
in urls.py
from my_app import views
eg code:
urls.py
from django.conf.urls import url
from django.contrib import admin
from pro1 import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
def home(request):
template="home.html"
context={}
return render(request,template,context)
guess it will solve the problem.
I am new to Django coding....i want to show whatever is there in area_distnce.html...below is my code
Thanks in advance....
Distance.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext
def area_distance(request):
return render_to_response('area_distance.html',locals(),context_instance=RequestContext(request))
urls.py
from django.conf.urls import patterns, include, url
from areas.Distance import area_distance
urlpatterns = patterns('',
url(r'^area/$','area_distance',name = 'area_distance'),)
When i run his it is giving
TypeError at /area/
'str' object is not callable
Looks like you have written view function area_distance() in Distance.py instead of views.py.
So you may want to update your urls.py to appropriately use that
url(r'^area/$', area_distance,name = 'area_distance'),)
---------------^ note no '...'
As you are doing from areas.Distance import area_distance this should work.
from django.http import HttpResponse
from django.template.context import RequestContext
def area_distance(request):
a= 1
b = a+1
t = get_template('area_distance.html')
c = RequestContext(request, locals())
return HttpResponse(t.render(c))
#url.py
from django.conf.urls import patterns, include, url
from areas.Distance import area_distance
urlpatterns = patterns('',
url(r'^area/$',area_distance,name = 'area_distance'),)
#template
{{ a }}
{{ b }}