单击链接时,Django没有显示正确的路径

时间:2018-02-15 21:44:45

标签: django url http-status-code-404

点击链接时显示的内容

  

找不到页面(404)

     

请求方法:GET请求URL:   http://localhost:8000/jobapplication/new/1

     

使用careal.urls中定义的URLconf,Django尝试了这些URL   模式,按此顺序:

     

^ $ [name ='landing-index']

     

^管理员/

     

^账户/

     

^任务管理器/

     

^ login / $ [name ='login']

  • 问题是,我不知道为什么它会以http://localhost:8000/jobapplication/new/1打开链接,应该是http://localhost:8000/taskmanager/jobapplication/new/1

  • 这就是我在urls.py中所拥有的

    from django.conf.urls import include, url
        from django.contrib import admin
        from django.conf import settings
        from django.contrib.auth import views as auth_views
        from landingpage import views as landingpage_views
    
    urlpatterns = [
            url(r'^$', landingpage_views.index, name='landing-index'),
            url(r'^admin/', admin.site.urls),
            url(r'^accounts/', include('allauth.urls')),
            url(r'^taskmanager/', include('taskmanager.urls')),
            url(r'^login/$', auth_views.login, name='login'),
    ]
    
  • 这是在app taskmanager的urls.py中

    from django.conf.urls import url
    from . import views
    from taskmanager.views import *
    
    app_name = 'taskmanager'
    urlpatterns = [
     # Task manager urls
       url(r'^$', JobApplicationIndex.as_view(), name='index'),
       url(r'jobapplication/add/(?P<jobpost_id>[0-9]+)/$', JobApplicationCreate.as_view(), name='jobapplication-add'),
       url(r'jobapplication/new/(?P<jobpost_id>[0-9]+)/$', views.JobApplicationAdd, name='jobapplication-new'),
       url(r'jobapplication/edit/(?P<jobpost_id>[0-9]+)/$', views.JobApplicationEdit, name='jobapplication-edit'),
      url(r'jobapplication/edit/(?P<pk>[0-9]+)/$', JobApplicationUpdate.as_view(), name='jobapplication-edit'),
      url(r'^jobapplication/(?P<pk>[0-9]+)/$', JobApplicationDetails.as_view(), name='jobapplication-detail'),
    
    # Company urls
    url(r'company/$', CompanyIndex.as_view(), name='company-index'),
    url(r'company/add/$', CompanyCreate.as_view(), name='company-add'),
    url(r'^company/(?P<pk>[0-9]+)/$', CompanyDetails.as_view(), name='company-detail'),
    
    # Job Post urls
    url(r'jobpost/$', JobPostIndex.as_view(), name='jobpost-index'),
    url(r'^jobpost/(?P<pk>[0-9]+)/$', JobPostDetails.as_view(), name='jobpost-detail'),
    
    # Statistics urls
    url(r'^kpi/$', views.kpi, name='kpi'),
    ]
    
  • 这就是我在taskmanager中的views.py中与jobapplication相关的内容

    # Job Application views
    class JobApplicationIndex(generic.ListView):
    template_name = 'taskmanager/jobapplication_index.html'
    
    def get_queryset(self):
        if self.request.user.is_authenticated:
            return   JobApplication.objects.filter(user=self.request.user.id).order_by('-created_at')
    
    
    class JobApplicationCreate(CreateView):
    model = JobApplication
    fields = ['jobpost', 'sent_date', 'deadline', 'success_rate']
    
    def get_initial(self):
        jobpost = get_object_or_404(JobPost,  id=self.kwargs.get('jobpost_id'))
        return {
            'jobpost':jobpost,
        }
    
    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(JobApplicationCreate, self).form_valid(form)
    
    class JobApplicationDetails(generic.DetailView):
    model = JobApplication
    
    class JobApplicationEdit(UpdateView):
    model = JobApplication
    #fields = ['jobpostid', 'is_favorite']
    #p = JobApplication.objects.get(id=jobpostid)
    #p.is_favorite = is_favorite
    #p.save()
    
    
    class JobApplicationUpdate(UpdateView):
    model = JobApplication
    fields = ['sent_date', 'deadline', 'success_rate']
    template_name_suffix = '_update_form'
    
    
    def JobApplicationAdd(request, jobpost_id):
     if request.method == 'GET' and request.user.is_authenticated:
        # If job app for this id exists, redirect to that job app page with a message
        if JobApplication.objects.filter(jobpost_id=int(jobpost_id)).exists():
            existing = JobApplication.objects.get(jobpost_id=int(jobpost_id))
            messages.add_message(request, messages.INFO, 'An application for this opening already exists.')
            return HttpResponseRedirect(reverse('taskmanager:jobapplication-detail', args=[existing.id]))
    
        jobapp = JobApplication(user=request.user, jobpost_id=int(jobpost_id), success_rate=50)
        jobapp.save()
    
    return HttpResponseRedirect(reverse('taskmanager:index'))
    

---这就是taskmanager工作中的所有其他链接,当你点击它们时,正确的路径被打开例如: -

  - http://localhost:8000/taskmanager/jobpost/
  - http://localhost:8000/taskmanager/jobpost/2/
  - http://localhost:8000/taskmanager/company/2/
  - http://localhost:8000/taskmanager/kpi/

1 个答案:

答案 0 :(得分:1)

尝试在正则表达式模式前添加上移,就像对正在运行的模式一样。

from django.conf.urls import url
from . import views
from taskmanager.views import *

app_name = 'taskmanager'
urlpatterns = [
    # Task manager urls
    url(r'^$', JobApplicationIndex.as_view(), name='index'),
    url(r'^jobapplication/add/(?P<jobpost_id>[0-9]+)/$', JobApplicationCreate.as_view(), name='jobapplication-add'),
    url(r'^jobapplication/new/(?P<jobpost_id>[0-9]+)/$', views.JobApplicationAdd, name='jobapplication-new'),
    url(r'^jobapplication/edit/(?P<jobpost_id>[0-9]+)/$', views.JobApplicationEdit, name='jobapplication-edit'),
    url(r'^jobapplication/edit/(?P<pk>[0-9]+)/$', JobApplicationUpdate.as_view(), name='jobapplication-edit'),
    url(r'^jobapplication/(?P<pk>[0-9]+)/$', JobApplicationDetails.as_view(), name='jobapplication-detail'),

    # Company urls
    url(r'^company/$', CompanyIndex.as_view(), name='company-index'),
    url(r'^company/add/$', CompanyCreate.as_view(), name='company-add'),
    url(r'^company/(?P<pk>[0-9]+)/$', CompanyDetails.as_view(), name='company-detail'),

    # Job Post urls
    url(r'^jobpost/$', JobPostIndex.as_view(), name='jobpost-index'),
    url(r'^jobpost/(?P<pk>[0-9]+)/$', JobPostDetails.as_view(), name='jobpost-detail'),

    # Statistics urls
    url(r'^kpi/$', views.kpi, name='kpi'),
]