I started using django for the first time today and I have been encountering one issue
I want to use the PDFNetPython3 library for my project in Django framework.
I ran the command pip install PDFNetPython3 and it is says its already installed, however Django isn't able to resolve the import
Error/Warning
Import "PDFNetPython3" could not be resolvedPylancereportMissingImports
This is the line below being shown as unresolved import.
from PDFNetPython3 import PDFDoc, Optimizer, SDFDoc, PDFNet
views.py
from http.client import HTTPResponse
from django.shortcuts import render, HttpResponse
# Create your views here.
def index(request):
return render(request, 'index.html')
def compress(request):
return render(request, 'compress.html')
def showName(request):
if request.method == 'POST':
user_initial_pdf = request.POST['user_initial_pdf']
from PDFNetPython3 import PDFDoc, Optimizer, SDFDoc, PDFNet
return HttpResponse("ok")
Python Version - 3.9.7
Django Version - 4.0.1
Please note : I havent created any virtual environment as of now cause I just started django and found out its a good practice to do so, can it be the cause django not able to resolve the library through pip ?
Help would be appreciated :)
Related
Hi I am new to Python/django and am using VS Code. Now I got python IntelliSense and pylance extension installed and most things are showing up on IntelliSense but some aren't.
How can I get it to work for everything?
I would very much appreciate some insight since it's driving me nuts...
request.POST from an imported lib not showing up
selected_meetup.participants not showing up nor is selected_meetup.participants.add
from urllib import request
from django.forms import SlugField
from django.shortcuts import render
from .models import Meetup
from .forms import RegistrationForm
# from django.http import HttpResponse
# Create your views here.
def index(request):
meetups = Meetup.objects.all()
return render(request, 'meetups/index.html', {
'meetups': meetups
})
def meetup_details(request, meetup_slug):
try:
selected_meetup = Meetup.objects.get(slug=meetup_slug)
if request == 'GET':
registration_form = RegistrationForm()
else:
registration_form = RegistrationForm(request.POST)
registration_form = RegistrationForm(request.)
if registration_form.is_valid():
participant = registration_form.save()
selected_meetup.participants.add(participant)
return render(request, 'meetups/meetup-detail.html', {
'meetup_found': True,
'meetup': selected_meetup,
'form': registration_form
})
except Exception as exc:
return render(request, 'meetups/meetup-detail.html', {'meetup_found': False
})
Update:
That's great guys, I now understand the type problem it's obvious now (only had so many extensions issues especially django/html) I got confused...
Now I added types to Meetup and Participant but selected_meetup.participants. still refused to show me its method ADD.
I assume the problem is the lib is written without types. Is there a way to solve that? because quite valuable to me to be able to see what's possible write away...
have you got solutions to that?
I would re-install the extensions, then make sure it is referencing the right version of python. If that doesn't work I would recommend to just type it out in full and look past the error.
You didn't declare what type of object request is in the programming process. Maybe you can declare request = request () in the code.The second is the same. vscode can't recognize the type of the selected_meetup you wrote.
You can hover over these two objects and find that the vscode prompt the type can be any.
I am trying to send data taken from a form to another views page to process it. I am using sessions to send that data. It works fine on a local host, the data is sent and received successfully, however, on a public server it crashes. I found a couple of posts regarding the same problem but their issue is mostly related to database integration which is not the case here.
This is the code that is causing the error its inside views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.templatetags.static import static
from plots.forms import plotform
from django.http import JsonResponse
#from rest_framework.views import APIView
#from rest_framework.response import Response
import numpy as np
import csv
import math
import json
def plots2(request):
if request.method == 'POST':
form = plotform(request.POST)
if form.is_valid():
frequency_min = form.cleaned_data['frequency_min']
frequency_max = form.cleaned_data['frequency_max']
distance = form.cleaned_data['distance']
humidity = form.cleaned_data['humidity']
temp = form.cleaned_data['temp']
request.session['frequency_min'] = frequency_min
request.session['frequency_max'] = frequency_max
request.session['distance'] = distance
request.session['humidity'] = humidity
request.session['temp'] = temp
return redirect('plots/')
form = plotform() #includes the form inside plots.html
#render(request, 'plots/plots2.html', {'form':form})
#return redirect(request.POST.get('next','plots/'))
#return redirect('plots/plots')
return render(request, 'plots/plots2.html', {'form':form})
def plots(request):
frequency_min_input = request.session['frequency_min']
frequency_max_input = request.session['frequency_max']
distance_input = request.session['distance']
humidity_input = request.session['humidity']
temperature_input = request.session['temp']
This is the error I get on the browser:
Request Method: POST
Request URL: http://142.93.51.83/
Django Version: 1.8.7
Exception Type: OperationalError
Exception Value:
no such table: django_session
Exception Location: /usr/lib/python2.7/dist-
packages/django/db/backends/sqlite3/base.py in execute, line 318
Python Executable: /usr/bin/python
Python Version: 2.7.12
Python Path:
['/home/django/django_project',
'/home/django/django_project',
'/usr/bin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']
Server time: Fri, 10 Aug 2018 00:49:07 +0000
I am not sure if you set session middleware properly.
First, Please check if you set sessionmiddleware properly.
MIDDLEWARE_CLASSES should have
'django.contrib.sessions.middleware.SessionMiddleware'
And add it to django apps. INSTALLED_APPS should have
'django.contrib.sessions'
And generate migration / migrate app again.
python manage.py makemigrations
python manage.py migrate
Solved with :
sudo chmod 777 project directory
sudo chmod 777 database file
run this
python manage.py migrate
python manage.py makemigrations
You might have even deleted your DB file by mistake, or it could have moved elsewhere in the directory structure. It is always advisable to keep a copy of the database file.
Frankly, this happened to me once. I was integrating someone else's code after committing mine, yet I replaced the DB file of her, because, it was of the same name (db.sqlite3), but there was no content in it. And I got the same error.
Thankfully, I had kept a copy and the world (Website) was fine again! Got saved.
I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).
I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:
python manage.py runserver
I get the following error:
Since I am new to Django, I do not know what is going on. Please help.
from django.http import HttpResponse
in your views file at the top
Put this import in your poll/views.py before using HttpResponse.
from django.http import HttpResponse
from django.http import HttpResponse
add this line on the top of polls/views.py file. I am new too, and had the same error. good Luck and i see u around.
in your polls/views.py
By default is :
from django.shortcuts import render
change to:
from django.shortcuts import render,HttpResponse
this will call the HttpResponse class
In my case the import was there, but when I called HttpsResponse I called it with small h as a typo instead of the capital H
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello!") #==> This one was with httpResponse so the same error been received.
I had imported HttpResponse and still got this error.
If you use Apache server as your primary server for web, try restarting Apache and reloading the page.
For me it was because I used singe quotes (') instead of double quotes (")
Check your import statement.
Check your function. I had "HttpsResponse" instead of "HttpResponse"
Good luck.
I'm trying to create migrations of a project which uses Rest Framework pagination.
But I'm getting error as Attribution error: 'module' object has no attribute BasePaginationSerializer. I have tried uninstalling-resinstalling the said versions of Python, Django and RestFramework. But I still get that error.
Here's the screenshot of terminal showing error.
Here's a chunk of the code present in paginator.py
import urlparse
from django.core.paginator import EmptyPage, Page, PageNotAnInteger, Paginator
from django.utils.http import urlencode
from rest_framework import serializers, pagination
class CustomPaginationSerializer(pagination.BasePaginationSerializer):#Here it shows the error.
meta = MetaSerializer(source='*')
results_field = 'objects'
Can someone help me out in getting this issue resolved?
Info:
- Here's the project which I'm trying to build. https://github.com/mozilla/zamboni
I'm using Ubuntu 15.10
Python - 2.7.10
Django - 1.8.7
Django Rest Framework doesn't provide anything like BasePaginationSerializer, that's why you're getting an error - because it doesn't exist. You probably want to use BasePagination
This is my first post on StackOverflow so Hello everyone.
I'm doing blog application to learn Python and Flask and I would like to launch it on Google App Engine. Unfortunately I have small problem with importing WTForms to the application. I'm currently using Flask 0.9, WTForms 1.0.1 and Flask-WTForms 0.8. I've added flaskext_wtf folder to root path of my project but I'm getting error from html5.py file.
File "/Users/lucas/Workspace/blog/flask_wtf/html5.py", line 1, in <module>
from wtforms import TextField
File "/Users/lucas/Workspace/blog/flask/exthook.py", line 86, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named flask.ext.wtf.wtforms
It looks like it tries to find wtforms inside the extension path instead of my project path. How can I inform the html5.py file to look for the wtforms in the root?
Here are sources of my project - https://bitbucket.org/lucas_mendelowski/wblog/src
I think your Virtualenv does not have the Flask-WTF module.
Write the following command in your command line under virtualenv
pip install Flask-WTF
or you may do this, but it is not recommended.
easy_install Flask-WTF
I think, I finally manage with this issue (but I'm not sure if it's the right way).
Change imports in these files:
1) flaskwtf/init_.py
From:
from flask.ext.wtf import html5
from flask.ext.wtf.form import Form
from flask.ext.wtf import recaptcha
from flask.ext.wtf.recaptcha.fields import RecaptchaField
from flask.ext.wtf.recaptcha.widgets import RecaptchaWidget
from flask.ext.wtf.recaptcha.validators import Recaptcha
To:
import html5
from form import Form
import recaptcha
from recaptcha.fields import RecaptchaField
from recaptcha.widgets import RecaptchaWidget
from recaptcha.validators import Recaptcha
2) flaskwtf/recaptcha/init_.py:
From:
from flask.ext.wtf.recaptcha import fields
from flask.ext.wtf.recaptcha import validators
from flask.ext.wtf.recaptcha import widgets
To:
import fields
import validators
import widgets
I've also posted a solution on github - https://github.com/rduplain/flask-wtf/issues/46#issuecomment-7376577
You have incorrect import. You are probably doing:
from flask.ext.wtf import wtforms
There is no such module.
Instead you should do:
from flask.ext.wtf import Form, TextField
If you want HTML5 widgets, just use the following. For example, you can import URLField as
from flask.ext.wtf.html5 import URLField
Your import is wrong. Also, make sure you install Flask-WTF which automatically installs WTForm.
As far as importing fields goes, according to the docs..
From version 0.9.0, Flask-WTF will not import anything from wtforms
you need to import fields from wtforms.
from flask_wtf import Form
from wtforms import TextField
from wtforms.validators import DataRequired
class MyForm(Form):
name = TextField('name', validators=[DataRequired()])
Please review the Flask-WTF quick start guide.