我是django的新手,我遵循udemy的教程(tweetme) 在设置rest_framework以将序列化数据加载为json时,我得到了以下错误:
TypeError:e.indexOf不是函数 in:jquery-3.3.1.min.js:2:82466
和:
源地图错误:请求失败,状态为404 资源网址:http://127.0.0.1:8000/static-directory/rest_framework/css/bootstrap.min.css 源地图URL:bootstrap.min.css.map
我只是使用序列化的类和简单的视图来显示我的数据,(我得到了显示但仍然有错误)
我的控制台中的输出:
[17 / Jun / 2018 08:50:24]“GET / HTTP / 1.1”200 5324
[17 / Jun / 2018 08:50:24]“GET /static-directory/rest_framework/js/csrf.js HTTP / 1.1”304 0 [17 / Jun / 2018 08:50:24]“GET /static-directory/rest_framework/js/ajax-form.js HTTP / 1.1”304 0 [17 / Jun / 2018 08:50:24]“GET /static-directory/rest_framework/js/jquery-3.3.1.min.js HTTP / 1.1”304 0
//其他.css和.js成功GET(200和3xx)
[17 / Jun / 2018 08:50:24]“GET /static-directory/rest_framework/css/bootstrap.min.css HTTP / 1.1”200 121200
[17 / Jun / 2018 08:50:24]“GET /static-directory/rest_framework/img/grid.png HTTP / 1.1”200 1458 [17 / Jun / 2018 08:50:25]“GET /static-directory/rest_framework/css/bootstrap.min.css.map HTTP / 1.1”404 1764
这是我的代码:
“/tweets/models.py”中的from django.conf import settings
from django.db import models
class Tweet(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default = 1,on_delete=models.CASCADE)
content = models.CharField(max_length=150, default="")
boolean = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
“/tweets/api/views.py”中的from rest_framework import generics
from .serializers import TweetSerializer
from tweets.models import Tweet
class TweetListApiView(generics.ListAPIView):
serializer_class = TweetSerializer
def get_queryset(self):
return Tweet.objects.all()
“/tweets/api/serializers.py”中的from rest_framework import serializers
from tweets.models import Tweet
class TweetSerializer(serializers.ModelSerializer):
class Meta:
model = Tweet
fields = [
"user",
"content",
"boolean",
"timestamp"
]
“/tweets/api/settings.py”中的STATIC_URL = '/static-directory/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static-storage"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),“static-serve”)
N.B:我没有js代码我说过我使用上面的代码,它使用django框架“rest_framework”中的js(框架使用jquery库) (抱歉错误的答案,我在手机上)
这是一个框架问题吗?
我开始了一个新的(干净的)django项目并完成了启动教程 django rest framework : http://www.django-rest-framework.org
在我的 urls.py :
中添加此内容from django.conf.urls import url, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
我感谢你的回答
答案 0 :(得分:2)
修正了TypeError: 在弄清楚这是兼容性问题后,我尝试安装djangorestframework 3.7.4,这是支持Django 2.0的第一个版本 (版本3.7.5,3.7.6和3.7.7也兼容) N.B:我使用的是djangorestframework 3.8.2
修复了找不到的bootstrap.min.css 404: 我在我的html上有这个:
<script src="{% static '/script/ajax.js' %}"></script>
更正是将链接更改为:
<script src="{% static 'script/ajax.js' %}"></script>
感谢大家的支持