我是Django的新手,我被困在Django views.py是两个字段中的forms.py第一个CharField第二个ImageField我的CharField成功渲染但ImageField没有呈现如何解决这个我想发送图像文件与推文没有保存在数据库请我解决这个问题谢谢
我的文件在这里 -
forms.py
from django import forms
class HomeForm(forms.Form):
tweet = forms.CharField()
img = forms.ImageField(label='Select a Image file',)
Views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import *
from django.views.generic import TemplateView
class Texting(TemplateView):
Template_name = 'app_temp/xyz.html'
def get(self, request):
form = HomeForm()
return render(request, self.Template_name, {'form': form})
def post(self, request):
access_token = 'XYZ'
access_token_secret = 'PQR'
consumer_key = 'LMN'
consumer_secret = 'ABC'
api = TwitterAPI(consumer_key, consumer_secret , access_token , access_token_secret )
form = HomeForm(request.POST , request.FILES)
if form.is_valid():
text = form.cleaned_data['tweet']
img = form.request.FILES['img']
r = api.request('statuses/update_with_media', {'status':text}, {'media[]':img})
return render(request,'app_temp/new_page.html')
HTML
{% extends 'login_base.html'%}
{% block content %}
<div class="container">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Tweet</button>
</form>
<h1>{{img}}</h1>
<h1>{{text}}</h1>
</div>
{% endblock %}
答案 0 :(得分:0)
<强> forms.py 强>
from django import forms
from app.models import *
class HomeForm(forms.Form , forms.ModelForm):
tweet = forms.CharField(max_length = 280 ,widget = forms.Textarea)
image = forms.ImageField(label='Select a Image file',)
<强> views.py 强>
from django.shortcuts import render
from django.views.generic import TemplateView
from TwitterAPI import *
from app.forms import *
class Texting(TemplateView):
def post(self, request):
form = HomeForm(request.POST,request.FILES)
if form.is_valid():
text = form.cleaned_data['tweet']
image = form.cleaned_data['image']
答案 1 :(得分:0)
<template>
<div>
<md-app>
<jg-header></jg-header>
</md-app>
</div>
</template>
已弃用。此API调用已由两步过程替换。首先,使用statuses/update_with_media
上传图片。其次,使用media/upload
使用媒体ID发布您的推文。您可以在https://github.com/geduldig/TwitterAPI/blob/master/examples/update_media.py找到完整的示例。
statuses/update