我目前正在使用django rest framework api,我有一个表单可以输入一些api信息。
我在提交表单后仍希望重定向,同时仍然使用请求库将数据发布到API网址。
但HttpResponseRedirect
因某种原因未触发重定向
这是我的views.py
from rest_frame.requests import reverse
import requests
def entry_new(request):
if request.method == 'POST':
form = EntryForm(request.POST)
if form.is_valid():
url = reverse("entry-list",request=request)
r = requests.post(url, data=request.POST, cookies=request.COOKIES)
## THIS IS WHAT ISN'T FIRING - it goes back to entry_new's URL
HttpResponseRedirect(reverse('index2'))
else:
form = EntryForm
return render(request, 'entry_new.html', {'form': form})
class entry_ViewSet(viewsets.ModelViewSet):
queryset = Entry.objects.all()
serializer_class= EntrySerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def perform_create(self, serializer):
serializer.partial = True
serializer.save(created_by=self.request.user, modified_by=self.request.user, modified_date=datetime.now())
我的serializers.py
class EntrySerializer(serializers.ModelSerializer):
created_by = serializers.ReadOnlyField(source='created_by.username')
modified_by = serializers.ReadOnlyField(source='modified_by.username')
class Meta:
model = Entry
fields = '__all__'
我的urls.py
router = DefaultRouter()
router.register(r'entry', views.entry_ViewSet, base_name="entry")
urlpatterns = [
url(r'^index2/$',views.index_two, name='index2'),
url(r'^cat_new/$', views.category_new, name='cat_new'),
url(r'^entry_new/$', views.entry_new, name='entry_new'),
url(r'^api/', include(router.urls)),
]