ImportError:无法导入名称Upload

时间:2017-02-08 17:16:33

标签: django django-rest-framework

我正在研究Django,django-rest项目,因为我已经搜索过这个问题,但我认为我有循环问题,但我不这么认为......

问题的相关部分在这里:

演示/ views.py

from django.shortcuts import render
from rest_auth.models import Upload, UploadForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
def home(request):
    if request.method=="POST":
        img = UploadForm(request.POST, request.FILES)
        if img.is_valid():
            img.save()
            return HttpResponseRedirect(reverse('imageupload'))
    else:
        img=UploadForm()
    images=Upload.objects.all()
    return render(request,'home.html',{'form':img,'images':images})

rest_auth / models.py

from django.conf import settings
from rest_framework.authtoken.models import Token as DefaultTokenModel
from .utils import import_callable

from django.db import models
from django.forms import ModelForm

class Upload(models.Model):
    pic = models.ImageField("Image", upload_to="images/")
    upload_date=models.DateTimeField(auto_now_add =True)


# FileUpload form class.
class UploadForm(ModelForm):
    class Meta:
        model = Upload

和 演示/ urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from django.conf import settings
from django.conf.urls.static import static
from views import home

from rest_framework_swagger.views import get_swagger_view
import os
PROJECT_DIR=os.path.dirname(__file__)

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
    url(r'^upload/$', views.home, name='imageupload'),
    url(r'^signup/$', TemplateView.as_view(template_name="signup.html"),
        name='signup'),

问题如下:

  

文件" /Desktop/zadnja/django-rest-auth/demo/demo/urls.py",   第6行,在       来自views import file" /Desktop/zadnja/django-rest-auth/demo/demo/views.py" ;, line   2,在       来自rest_auth.models import Upload,UploadForm ImportError:无法导入名称Upload

1 个答案:

答案 0 :(得分:0)

由于rest_auth/models.py未从demo/views.py

导入任何组件,因此您没有循环导入

我假设rest_auth是您项目中的应用。要导入UploadUploadForm,您应该使用绝对导入:

from project_name.rest_auth.models import Upload, UploadForm