上传Django的个人资料图片无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传Django的个人资料图片无法正常工作相关的知识,希望对你有一定的参考价值。

使用python 3和Django 3

[当我尝试上传图像时,显示此错误:“这些详细信息已存在”。

图像被复制到静态/媒体/ ...,但记录未添加到mysql表中。

我只是在其顶部添加了图片上传代码。在添加图像上传代码之前,没有问题。

任何人都知道如何解决它?非常感谢您的帮助。

i在setting.py文件上设置MEDIA DIR

这是我得到的错误

ProgrammingError at /workerreg/

(1064, "You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 
''image')values('Ernakulam','Alangad','sargehy','9788541255',
 '41','eagge','slfdgk' at line 1")

Request Method:     POST
Request URL:    http://127.0.0.1:8000/workerreg/
Django Version:     3.0.3
Exception Type:     ProgrammingError
Exception Value:    

(1064, "You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the right syntax to use  
near     
''image')values('Ernakulam','Alangad','sargehy','9788541255','41','eagge',
'slfdgk' at line 1")

Exception Location:     C:\Users\Kevin\AppData\Local\Programs\Pytho
\Python38\lib\site-packages\pymysql\err.py in raise_mysql_exception, line 
 109
Python Executable:  C:\Users\Kevin\AppData\Local\Programs\Python\Python38   
\python.exe
Python Version:     3.8.1
Python Path:    

  ['C:\\Users\\Kevin\\Desktop\\BCA MAIN PROJECT\\Baby Care',
  'C:\\Users\\Kevin\\AppData\\Local\\Programs\\Python\\Python38 
  \\python38.zip',
  'C:\\Users\\Kevin\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
  'C:\\Users\\Kevin\\AppData\\Local\\Programs\\Python\\Python38\\lib',
  'C:\\Users\\Kevin\\AppData\\Local\\Programs\\Python\\Python38',
   'C:\\Users\\Kevin\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-
  packages']

   Server time:     Thu, 12 Mar 2020 09:04:57 +0000

Views.py

def workerreg(request):

if request.POST:
    district=request.POST.get("district")
    panchayath=request.POST.get("panchayathlist")
    wnamee=request.POST.get("wnamee")
    wphnu=request.POST.get("wphnu")
    wan=request.POST.get("wan")
    waddress=request.POST.get("waddress")
    wmail=request.POST.get("wmail")
    qul=request.POST.get("qul")
    uploaded_file_url=""
    if request.FILES.get("image"):
        myfile=request.FILES.get("image")
        fs=FileSystemStorage()
        filename=fs.save(myfile.name , myfile)
        uploaded_file_url = fs.url(filename)


    worker_exists="select count(*) from worker_reg where district='"+str(district)+"' and panchayath='"+str(panchayath)+"' and ward_no='"+str(wan)+"' or phone_no='"+str(wphnu)+"'"
    print("-------"+worker_exists+"-------")
    c.execute(worker_exists)
    exisist_data=c.fetchone()
    print(exisist_data)
    try:
        if exisist_data[0]>0:
            message="ashaworker already exisist"
            return render(request,"workerreg.html","message":message)
        else:

            random_num=random.randrange(1000,10000,2)
            print("--------password---- "+str(random_num))
            password="worker"+str(random_num)
            print(password)
            worker_insert="insert into worker_reg(`district`,`panchayath`,`worker_name`,`phone_no`,`ward_no`,`address`,`email`,`qualification`,'image')values('"+str(district)+"','"+str(panchayath)+"','"+str(wnamee)+"','"+str(wphnu)+"','"+str(wan)+"','"+str(waddress)+"','"+str(wmail)+"','"+str(qul)+"','"+str(uploaded_file_url)+"')"
            worker_login="insert into login(userid,username,password,status,usertype)values((select max(wrkr_id) from worker_reg),'"+str(wphnu)+"','"+str(password)+"','1','worker')"
            c.execute(worker_insert)
            conn.commit()
            c.execute(worker_login)
            conn.commit()
            msg="Congratulations!!! Government added you as ashaworker in the "+panchayath+" panchayath for ward number :"+wan+" Username :"+wphnu+" Password: "+password
            sendsms(wphnu,msg)
            message="Added Successfully"
            return render(request,"workerreg.html","message":message)
    except:
        message="Such details already exisist"
        return render(request,"workerreg.html","message":message)

return render(request,'workerreg.html')

upload.html

%if message%
    <script>

    alert('message');

    </script>
    %endif%

        <form action="#" method="post"  enctype="multipart/form-data">
                % csrf_token %



          <label style='position: relative; top: 10px;' for="file-upload" class="custom-file-upload2">
      Select Photo
       </label>        
      <input type="file" id="file-upload" name="image" required=""/> <br/>    <br/><br/>



        <select name="district" id="district">
                                        <option>--Choose District--</option>
                                     <option>Kasargode</option>
                                     <option>Kannur</option>
                                     <option>Vayanad</option>
                                     <option>Malappuram</option>
                                     <option>Kottayam</option>
                                     <option>Kozhikod</option>
                                     <option>Ernakulam</option>
                                     <option>Pathanamthitta</option>
                                     <option>Alappuzha</option>
                                     <option>Idukki</option>
                                     <option>Palakkad</option>
                                     <option>Thrissur</option>
                                     <option>Kollam</option>
                                     <option>Thiruvananthapuram</option>
                 </select><br><br>


        <select  id="panchayathlist" name="panchayathlist">
            <option>--Choose Panchayath--</option>      

                 </select><br><br>
    <input placeholder=" Worker Name" pattern="[A-Za-z]1[A-Za-z\s]2,32"  name="wnamee" type="text" required=""><br><br>
    <input placeholder="Phone number" name="wphnu" type="text" pattern="[789][0-9]9" required=""><br><br>
    <input placeholder="Ward number" name="wan" type="number" maxlength="3" required=""><br><br>
    <textarea placeholder="Address of worker" name="waddress" style="width: 50%;"></textarea><br><br>
    <input placeholder="Email" name="wmail" type="email" required=""><br><br>
    <input placeholder="Qualification" pattern="[A-Za-z]1[A-Za-z\s]2,32" name="qul" type="text" required=""><br><br>




    <input type="submit" value="Register"><br><br>
答案

已解决。

这是撇号问题

我写了这个

'image'

实际修正为

`image`

以上是关于上传Django的个人资料图片无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 cloudinary 更新个人资料图片

无法在 FirebaseStorage 上上传图片

django:在添加新照片时删除以前的照片

网站后台编辑器(ewebeditor)无法上传,不管是图片、文档、视频或者其他资料

上传的图片不显示在网页上

(Django)无法为用户头像上传图像