我试图让我的用户发布到项目页面上,但是在提交帖子表单时,我得到了404并且消息“没有用户匹配给定的查询”。我也不确定我的form_valid部分是否正确。
我希望我的用户能够在他们拥有的不同项目页面上发布帖子,并且每个帖子都与正确的项目相关。
非常感谢任何帮助和示例代码!
视图
<ons-sliding-menu
var="app.slidingMenu"
menu-page="sliding_menu.html"
side="right"
main-page="about-us-aboutInfo.html"
type="push"
max-slide-distance="260px"
swipeable="false">
</ons-sliding-menu>
<ons-template id="about-us-aboutInfo.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-back-button>Back</ons-back-button>
</div>
<div class="center animated fadeIn">{{ title }}</div>
<div class="right">
<ons-toolbar-button ng-click="app.slidingMenu.toggle()">
<i class="ion-navicon" style="font-size:32px; vertical-align:-6px;"></i>
</ons-toolbar-button>
</div>
</ons-toolbar>
<div class="about-us-aboutInfo">
<div ui-view class="container">
<div class="content animated fadeIn">
<h1 class="text-heading">{{ title }}</h1>
<div class="animated fadeIn">
<div ui-view ng-repeat="section in Source.sections">
<h1 class="text-heading">{{ section.heading }}</h1>
<p ng-repeat="paragraph in section.paragraphs">
{{ paragraph }}
</p>
</div>
</div>
</div>
</div>
</div>
</ons-page>
</ons-template>
网址
class NewPost(CreateView):
model = ProjectPost
form_class = ProjectPostForm
template_name = 'howdidu/new_post.html'
def form_valid(self, form):
self.object = form.save(commit=False)
project = UserProject.objects.get(slug=self.kwargs["slug"])
self.object.project = project
self.object.save()
return super(NewPost, self).form_valid(form)
def get_success_url(self):
project_username = self.request.user.username
project_slug = self.object.slug
return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})
模型
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/new_post/$', login_required(views.NewPost.as_view()), name='new_post'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/update_project/$', login_required(views.UpdateProject.as_view()), name='update_project'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/delete_project/$', login_required(views.DeleteProject.as_view()), name='delete_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
形式
class UserProject(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
project_overview = models.CharField(max_length=1000)
project_picture = models.ImageField(upload_to='project_images', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
project_views = models.IntegerField(default=0)
project_likes = models.IntegerField(default=0)
project_followers = models.IntegerField(default=0)
slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(UserProject, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
class ProjectPost(models.Model):
project = models.ForeignKey(UserProject)
title = models.CharField(max_length=100)
post_overview = models.CharField(max_length=1000)
date_created = models.DateTimeField(auto_now_add=True)
post_views = models.IntegerField(default=0)
post_likes = models.IntegerField(default=0)
模板
#form to add project details
class UserProjectForm(forms.ModelForm):
class Meta:
model = UserProject
fields = ('title', 'project_picture', 'project_overview')
#form to create a post
class ProjectPostForm(forms.ModelForm):
class Meta:
model = ProjectPost
fields = ('title', 'post_overview')
答案 0 :(得分:0)
您将表单发布到/ new_project /,但是在urls.py中不存在。你可能意味着/ create_project /.