ValueError:无法查询“”:必须是“User”实例

时间:2017-11-06 19:01:10

标签: python django

我正在尝试创建一个故事分享网站,用户可以访问特定作者的个人资料,查看作者撰写的所有故事。但是我收到了错误。

/ storyauthor / 1上的

ValueError 无法查询" yumin":必须是"用户"实例

这是我的模特:

    private String[] array;
    private WebView myWebView;
    private ProgressDialog progress;;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        progress = new ProgressDialog(TestActivity.this);
        progress.setTitle("Loading");
        progress.setMessage("Wait until site has finished loading.");
        progress.setCancelable(true);
        progress.show();

        myWebView = (WebView) findViewById(R.id.myWebView);
        array = getApplicationContext().getResources().getStringArray(R.array.myArray);
        final String randomStr = array[new Random().nextInt(array.length)];

        Log.d("TAG: ", randomStr);

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.getSettings().setSupportMultipleWindows(true);
        myWebView.loadUrl(randomStr);

        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                if(progress.isShowing()){
                    progress.cancel();
                }
            }
        });
    }

视图:

class StoryAuthor(models.Model):
    """
    Model representing a author.
    """
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    def get_absolute_url(self):
        """
        Returns the url to access a particular story-author instance.
        """

        return reverse('stories_by_author', args=[str(self.id)])

    def __str__(self):
        """
        String for representing the Model object.
        """
        return self.user.username

urls.py:

from django.views import generic

from django.views.generic import ListView

class StoryListbyAuthorView(generic.ListView):

"""
Generic class-based view for a list of stories posted by a particular 
StoryAuthor.
"""
model = Story
template_name = 'story/story_list_by_author.html'

def get_queryset(self):
    """
    Return list of Story objects created by StoryAuthor (author id specified in URL)
    """
    id = self.kwargs['pk']
    target_author=get_object_or_404(StoryAuthor, pk = id)
    return Story.objects.filter(author=target_author)

def get_context_data(self, **kwargs):
    """
    Add StoryAuthor to context so they can be displayed in the template
    """
    # Call the base implementation first to get a context
    context = super(StoryListbyAuthorView, self).get_context_data(**kwargs)
    # Get the storyauthor object from the "pk" URL parameter and add it to the context
    context['author'] = get_object_or_404(StoryAuthor, pk = self.kwargs['pk'])
    return context

0 个答案:

没有答案