如果url中没有pk,Django DetailView会显示最后一个

时间:2018-03-14 10:46:19

标签: python django django-class-based-views

使用基于类的DetailView可以说"如果没有指定pk"调用url时,只需使用MyModel.object.last()作为对象引用?

url(r'^(?:(?P<pk>\d+)/)?detail/$', views.MyDetailView.as_view()),

class MyDetailView(DetailView):
    model = MyModel
    if self.kwargs['pk'] == False:
       self.object = MyModel.object.last()

我正在运行Django 1.11

1 个答案:

答案 0 :(得分:4)

您可以覆盖03-14 20:42:52.062 17192-17192/com.demo.common.recordwritertest W/System.err: java.io.IOException: Permission denied 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.io.File.createNewFile(File.java:948) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.demo.common.recordwritertest.utils.FileWriter.appendData(FileWriter.java:25) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.demo.common.recordwritertest.com.demo.common.recordwritertest.asynctasks.AuthenticationAsyncTask.onPostExecute(AuthenticationAsyncTask.java:119) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.demo.common.recordwritertest.com.demo.common.recordwritertest.asynctasks.AuthenticationAsyncTask.onPostExecute(AuthenticationAsyncTask.java:27) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:667) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.Looper.loop(Looper.java:154) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.lang.reflect.Method.invoke(Native Method) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 方法。请注意,您应该检查get_object中是否设置了pk,而不是self.kwargs是否等于{。}}。

False