使用简单的api。一切似乎都很好,但是在按下按钮之后,脚本应该将数据发送到模型。现在,它向我提示错误404和以前的append_slash,但我将其设置为false仍然无法正常工作。我在这个框架中很新鲜。赞赏建议。
仅注意到单击按钮后,我的http栏转到/ index,该索引不存在。我的表单模板位于原始localhost上。那我该如何传递数据?
views.py
def index(request):
url = 'http://www.omdbapi.com/?t={}&apikey=My key is here'
if request.method == 'POST':
form = MovieForm(data=request.POST)
form.save()
else:
form = MovieForm()
movies = Movie.objects.all() #// fetch all objects
movies_data = [] #//// array for movies and their details
for movie in movies:
r = requests.get(url.format(movie)).json() #// gets details from api
movies_main = {
'title': movie.title,
'director': r['Director'],
'rate': r['imdbRating'],
}
movies_data.append(movies_main)
context = {
'form': form,
'movies_data': movies_data,
}
return render(request, 'movies/movies.html', context)
主要网址
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('movies.urls'))
]
应用网址
urlpatterns = [
path('', views.index),
]