开发者社区> 问答> 正文

django 发布信息后 TypeError at /jyinfo/1  __i?报错

django 发布信息后 TypeError at /jyinfo/1  init() takes exactly 1 argument (3 given)? 400 报错

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/jyinfo/1

Django Version: 1.5.5 Python Version: 2.7.6 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.admindocs', 'tongzhi') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware')

Traceback: File "E:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response 115. response = callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /jyinfo/1 Exception Value: init() takes exactly 1 argument (3 given)

 

 

 

django 发布信息后 TypeError at /jyinfo/1  __init__() takes exactly 1 argument (3 given)

 

附:models.py

#交友
class JiaoYou(models.Model):
    jycategory = models.ForeignKey(JyCategory, verbose_name=u'交友分类')
    title = models.CharField(max_length='60')
    content = models.TextField()
    user= models.ForeignKey(User)
    ip = models.IPAddressField()
    datetimeinfo = models.DateTimeField(auto_now_add=True)
    clickcount = models.IntegerField(default=0)
    addr_dz = models.CharField(max_length=50, blank=True)
    phone_dh = models.CharField(blank=True, max_length='50')
    qq = models.IntegerField(blank=True, null=True)
    lxr_user = models.CharField(blank=True, max_length='10')
    is_top  = models.IntegerField(default=0)
    infopic1 = models.FileField(upload_to='./pic/%Y/%m/%d/%H/', blank=True, null=True)
    infopic2 = models.FileField(upload_to='./pic/%Y/%m/%d/%H/', blank=True, null=True)
    infopic3 = models.FileField(upload_to='./pic/%Y/%m/%d/%H/', blank=True, null=True)

    def __unicode__(self):
        return u'用户%s发表的标题为%s的交友信息' % (self.user.username, self.title)
    class Meta:
        verbose_name_plural = u'交友信息'

 

#交友信息发布函数
@login_required
def jiaoyou_post(request):
    if request.method == 'POST':
        form = JiaoYouPostForm(request.POST, request.FILES)
        if form.is_valid():
            newinfo = JiaoYou(
                title = form.cleaned_data['title'],
                content = form.cleaned_data['content'],
                infopic1 = form.cleaned_data['infopic1'],
                infopic2 = form.cleaned_data['infopic2'],
                infopic3 = form.cleaned_data['infopic3'],
                user = request.user,
                jycategory = form.cleaned_data['jycategory'],
                addr_dz = form.cleaned_data['addr_dz'],
                ip = request.META['REMOTE_ADDR'],
                phone_dh = form.cleaned_data['phone_dh'],                
                qq = form.cleaned_data['qq'],
                lxr_user = form.cleaned_data['lxr_user'],
                )
            newinfo.save()
            return HttpResponseRedirect(reverse('jyinfo', args=[newinfo.id]))
        else:
            return HttpResponse("你填写的信息有误,请返回修改后重新发布!")
    else:
        form = JiaoYouPostForm()
        variables = RequestContext(request, {'form':form})
        return render_to_response('jy/jiaoyou_post.html', variables)

 

 

这样发布的信息。可以发布到数据库里面。

但是用DatilView调用展示的时候就出现上面的错误

看出错提示好像是 多传了个参数? 但是实在找不出是哪里出了问题了。

这是views

#交友信息页面
class jyinfo(DetailView):
    model = JiaoYou  
    template_name = 'jy/jyinfo.html'
    def get_object(self):
        object1 = super(jyinfo, self).get_object()
        object1.clickcount += 1
        object1.save()
        return object1

 

展开
收起
优选2 2020-06-04 18:05:22 613 0
1 条回答
写回答
取消 提交回答
  • 把urls.py, response返回的代码贴出来看看######

    引用来自“_yjp”的评论

    把urls.py, response返回的代码贴出来看看
    这是urls.py
    # -*- coding:utf-8 -*-
    from django.conf.urls import patterns, include, url
    from django.views.generic import DetailView, ListView, TemplateView
    from tongzhi.models import *
    from tongzhi.views import *
    import os
    import settings
    from django.contrib import admin
    admin.autodiscover()
    sitemedia1 = os.path.join(os.path.dirname(__file__), '../site_media').replace('\\', '/')
    
    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'tjxxw.views.home', name='home'),
        # url(r'^tjxxw/', include('tjxxw.foo.urls')),
    
        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        url(r'^tz/reg/$', 'tongzhi.views.register_tz'),
        url(r'^accounts/register/ok/$', 'tongzhi.views.reg_ok'),
        url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
        url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
        url(r'^accounts/password/reset/$', 'django.contrib.auth.views.password_reset'),
        url(r'^accounts/password/reset/done/$', 'django.contrib.auth.views.password_reset_done'),
        url(r'^password/reset/complete/$', 'django.contrib.auth.views.password_reset_complete', name='auth_password_reset_complete'),
        url(r'^accounts/change-password/$', 'django.contrib.auth.views.password_change', {'template_name':'registration/password_change.html'}),
        url(r'^accounts/change-done/$', 'django.contrib.auth.views.password_change_done', {'template_name':'registration/password_change_success.html'}),
        url(r'^jypost/$', 'tongzhi.views.jiaoyou_post', name='jiaoyou_post'),
        url(r'^tzpost/$', 'tongzhi.views.tzinfo_post', name='tzinfo_post'),
        url(r'^staticfiles/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATICFILES_DIRS, 'show_indexes':True}),
        url(r'^$', 'tongzhi.views.indexlist', name='indexlist'),
        url(r'^jylist/(?P<pk>\d+)$', 'tongzhi.views.jylist', name='jylist'),
                           
        url(r'^jyinfo/(?P<pk>\d+)/$', 'tongzhi.views.jyinfo', name='jyinfo'),
        url(r'^tzlist/(?P<pk>\d+)$', 'tongzhi.views.tzlist', name='tzlist'),
        url(r'^tzinfo/(?P<pk>\d+).html$', 'tongzhi.views.tzinfo', name='tzinfo'),
        url(r'^admin/', include(admin.site.urls)),
    )
    
    
    
    if settings.DEBUG:
        urlpatterns += patterns('',
                                url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),
                                url(r'^img/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT}),
    			    url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': sitemedia1}),
                                )


    ######response返回的代码 是指的哪块啊?######

    urls.py 的这行修改一下试试

        url(r'^jyinfo/(?P<pk>\d+)/$', 'tongzhi.views.jyinfo', name='jyinfo'),

    修改为

        url(r'^jyinfo/(?P<pk>\d+)/$', jyinfo.as_view(), name='jyinfo'),

    ######

    多谢...

    我就说.哪里有点小问题...

    原来是as_views..这里忘了views里用的是通用视图了.

    2020-06-05 13:11:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载