Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django 127.0.0.1:8000/管理员/停止工作_Python_Django_Url_Http Status Code 404_Admin - Fatal编程技术网

Python Django 127.0.0.1:8000/管理员/停止工作

Python Django 127.0.0.1:8000/管理员/停止工作,python,django,url,http-status-code-404,admin,Python,Django,Url,Http Status Code 404,Admin,不确定我做了什么来破坏管理站点,但转到127.0.0.1:8000/admin/不起作用,并在下面的屏幕截图中显示错误: 以下是两个URL.py文件: myproject/url.py from django.conf.urls import include, url from django.contrib import admin import product_app.urls urlpatterns = [ url(r'^admin/', admin.site.urls),

不确定我做了什么来破坏管理站点,但转到127.0.0.1:8000/admin/不起作用,并在下面的屏幕截图中显示错误:

以下是两个URL.py文件:

myproject/url.py

from django.conf.urls import include, url
from django.contrib import admin
import product_app.urls

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(product_app.urls)),
]
以及产品_app url.py:

from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
from . import views
from .views import *

urlpatterns = [

    url(r'^$', views.HomePage.as_view(), name='home'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^subscribe/$', views.subscribe, name='subscribe'),
    url(r'^products/$', views.products, name = 'products'),
    url(r'^product/$', ProductListView.as_view(), name='product_list'),
    url(r'^user/(\w+)/$', views.profile, name='profile'),
    url(r'post_url/', views.post_product, name='post_product'),
    url(r'^([0-9]+)/$', views.detail, name = 'detail'),
    url(r'^login/$', views.login_view, name='Login'),
    url(r'^logout/$', views.logout_view, name='Logout'),
    url(r'^like_product/$', views.like_product, name='like_product' ),

    url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),

    url(r'^(?P<pk>\d+)/edit/$', PostUpdateView.as_view(), name='product-edit'),
    url(r'^(?P<pk>\d+)/delete/$', PostDeleteView.as_view(), name='product-delete'),
]

if settings.DEBUG:
    urlpatterns += [
        url(r'^product/(?P<path>.*)$', serve, {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]
…最后,my views.py:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

from .models import Product #, HashTag
from .forms import ProductForm, LoginForm, ContactForm, SubscribeForm, EditProfileForm
from django.views import generic

# edit / delete views
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic.edit import UpdateView, DeleteView

# contact and subscribe forms
from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth import get_user_model
from . import forms
from django.shortcuts import render

from django.views.generic.list import ListView


class HomePage(generic.TemplateView):
    template_name = "index.html"

    def get_context_data(self, *args, **kwargs):
        context=super(HomePage, self).get_context_data(*args, **kwargs)
        context['form'] = ContactForm
        return context


def products(request):
    username = request.GET.get('username',None)
    user = None
    if username:
        try:
            user = User.objects.get(username=username)
        except (User.DoesNotExist, User.MultipleObjectsReturned):
            pass
    if user:
        return Product.objects.filter(user=user)
    else:
        products = Product.objects.all()
    form = ProductForm()
    return render(request, 'products.html', {'products': products, 'form':form})


class ProductListView(ListView):
    template_name = 'product_list.html'
    context_object_name = 'product_list'
    paginate_by = None

    def get_queryset(self):
        username = self.request.GET.get('username',None)
        user = None
        if username:
            try:
                user = User.objects.get(username=username)
            except (User.DoesNotExist, User.MultipleObjectsReturned):
                pass
        if user:
            return Product.objects.filter(user=user)
        return Product.objects.none()


def post_product(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = ProductForm(data = request.POST, files = request.FILES)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            product = form.save(commit = False)
            product.user = request.user
            product.likes = 0
            product.save()
        # redirect to a new URL:
        return HttpResponseRedirect('/products')

def detail(request, product_id):
    product = Product.objects.get(id=product_id)
    #hashtags = HashTag.objects.filter(product=product_id)
    return render(request, 'detail.html', {'product': product})

def profile(request, username):
    user = get_object_or_404(User, username=username)
    products = Product.objects.filter(user=user)
    if not request.user == user:
        return render(request, 'no.html')
    else:
        return render(request, 'profile.html', {'user':user,'products': products})

def edit_profile(request):
    user = request.user
    products = Product.objects.filter(user=user)
    form = EditProfileForm(request.POST or None, initial={'first_name':user.first_name, 'last_name':user.last_name})
    if request.method == 'POST':
        if form.is_valid():
            user.first_name = request.POST['first_name']
            user.last_name = request.POST['last_name']
            user.save()
            return render(request, 'profile.html', {'user':user,'products': products})
    context = {"form": form}
    return render(request, "edit_profile.html", context)


def like_product(request):
    product_id = request.POST.get('product_id', None)

    likes = 0
    if (product_id):
        product = Product.objects.get(id=int(product_id))
        if product is not None:
            likes = product.likes + 1
            product.likes = likes
            product.save()

    return HttpResponse(likes)


def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username=form.cleaned_data['username']
            password=form.cleaned_data['password']
            user = authenticate(username=username, password=password)

            if user is not None:
                # the password verified for the user
                if user.is_active:
                    print("User is valid, active and authenticated")
                    login(request, user)
                    products = Product.objects.filter(user=user)
                    return render(request, 'profile.html', {'user':user,'products': products})
                else:
                    print("The password is valid, but the account has been disabled!")
            else:
                # the authentication system was unable to verify the username and password
                print("The username and password were incorrect.")

    else:
        form = LoginForm()
    return render(request, 'login.html', {'form': form})


def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')


class PostUpdateView(UpdateView):
   model = Product
   form_class = ProductForm
   template_name = 'edit_product.html'

   def form_valid(self, form):
      self.object = form.save(commit=False)
      # Any manual settings go here
      self.object.save()
      # return HttpResponseRedirect(self.object.get_absolute_url())
      return redirect ('products')

   @method_decorator(login_required)
   def dispatch(self, request, *args, **kwargs):
     return super(PostUpdateView, self).dispatch(request, *args, **kwargs)


class PostDeleteView(DeleteView):
   model = Product
   template_name = 'product_confirm_delete.html'

   def get_success_url(self):
      return reverse ('products')

   @method_decorator(login_required)
   def dispatch(self, request, *args, **kwargs):
      return super(PostDeleteView, self).dispatch(request, *args, **kwargs)

User = get_user_model()

def subscribe(request):
    form_class = SubscribeForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_email = request.POST.get('contact_email', '')

            # Email the profile with the
            # contact information
            template = get_template('contact/subscribe_template.txt')
            context = dict({'contact_name': contact_name, 'contact_email': contact_email,})

            content = template.render(context)

            email = EmailMessage(
                "New subscribe form submission",
                content,
                "Your website" +'',
                ['steve@steve-shead.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return render(request, 'contact/thank_you_subscribe.html')

    return render(request, 'contact/subscribe.html', {
        'form': form_class,
    })

def contact(request):
    form_class = ContactForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_email = request.POST.get('contact_email', '')
            form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
            template = get_template('contact/contact_template.txt')
            context = dict({'contact_name': contact_name, 'contact_email': contact_email, 'form_content': form_content,})

            content = template.render(context)

            email = EmailMessage(
                "New contact form submission",
                content,
                "Your website" +'',
                ['steve@steve-shead.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return render(request, 'contact/thank_you.html')

    return render(request, 'contact/contact.html', {
        'form': form_class,
    })

我不知道我改变了什么,使管理网站不工作-任何帮助,感谢收到

检查您的
ROOT\u URLCONF
设置-它需要设置为
myproject.url
,但看起来它当前设置为
product\u app.url

检查您的
ROOT\u URLCONF
设置-它需要设置为
myproject.url
,但看起来它当前设置为
product\u app.URL

在设置文件中设置的
ROOT\u URLCONF
是什么?您需要设置默认的URL配置文件
myproject.URL
,而不是
product\u app.URL
@solarismoke-ugh-ROOT\u URLCONF错误-可能是我唯一没有检查的该死设置。非常感谢。如果你把这个作为解决方案,我会把它标记为正确的!在您的设置文件中,
ROOT\u URLCONF
设置为什么?您需要设置默认的URL配置文件
myproject.URL
,而不是
product\u app.URL
@solarismoke-ugh-ROOT\u URLCONF是错误的-可能是我没有检查的唯一错误设置。非常感谢。如果你把这个作为解决方案,我会把它标记为正确的!
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

from .models import Product #, HashTag
from .forms import ProductForm, LoginForm, ContactForm, SubscribeForm, EditProfileForm
from django.views import generic

# edit / delete views
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic.edit import UpdateView, DeleteView

# contact and subscribe forms
from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth import get_user_model
from . import forms
from django.shortcuts import render

from django.views.generic.list import ListView


class HomePage(generic.TemplateView):
    template_name = "index.html"

    def get_context_data(self, *args, **kwargs):
        context=super(HomePage, self).get_context_data(*args, **kwargs)
        context['form'] = ContactForm
        return context


def products(request):
    username = request.GET.get('username',None)
    user = None
    if username:
        try:
            user = User.objects.get(username=username)
        except (User.DoesNotExist, User.MultipleObjectsReturned):
            pass
    if user:
        return Product.objects.filter(user=user)
    else:
        products = Product.objects.all()
    form = ProductForm()
    return render(request, 'products.html', {'products': products, 'form':form})


class ProductListView(ListView):
    template_name = 'product_list.html'
    context_object_name = 'product_list'
    paginate_by = None

    def get_queryset(self):
        username = self.request.GET.get('username',None)
        user = None
        if username:
            try:
                user = User.objects.get(username=username)
            except (User.DoesNotExist, User.MultipleObjectsReturned):
                pass
        if user:
            return Product.objects.filter(user=user)
        return Product.objects.none()


def post_product(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = ProductForm(data = request.POST, files = request.FILES)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            product = form.save(commit = False)
            product.user = request.user
            product.likes = 0
            product.save()
        # redirect to a new URL:
        return HttpResponseRedirect('/products')

def detail(request, product_id):
    product = Product.objects.get(id=product_id)
    #hashtags = HashTag.objects.filter(product=product_id)
    return render(request, 'detail.html', {'product': product})

def profile(request, username):
    user = get_object_or_404(User, username=username)
    products = Product.objects.filter(user=user)
    if not request.user == user:
        return render(request, 'no.html')
    else:
        return render(request, 'profile.html', {'user':user,'products': products})

def edit_profile(request):
    user = request.user
    products = Product.objects.filter(user=user)
    form = EditProfileForm(request.POST or None, initial={'first_name':user.first_name, 'last_name':user.last_name})
    if request.method == 'POST':
        if form.is_valid():
            user.first_name = request.POST['first_name']
            user.last_name = request.POST['last_name']
            user.save()
            return render(request, 'profile.html', {'user':user,'products': products})
    context = {"form": form}
    return render(request, "edit_profile.html", context)


def like_product(request):
    product_id = request.POST.get('product_id', None)

    likes = 0
    if (product_id):
        product = Product.objects.get(id=int(product_id))
        if product is not None:
            likes = product.likes + 1
            product.likes = likes
            product.save()

    return HttpResponse(likes)


def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username=form.cleaned_data['username']
            password=form.cleaned_data['password']
            user = authenticate(username=username, password=password)

            if user is not None:
                # the password verified for the user
                if user.is_active:
                    print("User is valid, active and authenticated")
                    login(request, user)
                    products = Product.objects.filter(user=user)
                    return render(request, 'profile.html', {'user':user,'products': products})
                else:
                    print("The password is valid, but the account has been disabled!")
            else:
                # the authentication system was unable to verify the username and password
                print("The username and password were incorrect.")

    else:
        form = LoginForm()
    return render(request, 'login.html', {'form': form})


def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')


class PostUpdateView(UpdateView):
   model = Product
   form_class = ProductForm
   template_name = 'edit_product.html'

   def form_valid(self, form):
      self.object = form.save(commit=False)
      # Any manual settings go here
      self.object.save()
      # return HttpResponseRedirect(self.object.get_absolute_url())
      return redirect ('products')

   @method_decorator(login_required)
   def dispatch(self, request, *args, **kwargs):
     return super(PostUpdateView, self).dispatch(request, *args, **kwargs)


class PostDeleteView(DeleteView):
   model = Product
   template_name = 'product_confirm_delete.html'

   def get_success_url(self):
      return reverse ('products')

   @method_decorator(login_required)
   def dispatch(self, request, *args, **kwargs):
      return super(PostDeleteView, self).dispatch(request, *args, **kwargs)

User = get_user_model()

def subscribe(request):
    form_class = SubscribeForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_email = request.POST.get('contact_email', '')

            # Email the profile with the
            # contact information
            template = get_template('contact/subscribe_template.txt')
            context = dict({'contact_name': contact_name, 'contact_email': contact_email,})

            content = template.render(context)

            email = EmailMessage(
                "New subscribe form submission",
                content,
                "Your website" +'',
                ['steve@steve-shead.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return render(request, 'contact/thank_you_subscribe.html')

    return render(request, 'contact/subscribe.html', {
        'form': form_class,
    })

def contact(request):
    form_class = ContactForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_email = request.POST.get('contact_email', '')
            form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
            template = get_template('contact/contact_template.txt')
            context = dict({'contact_name': contact_name, 'contact_email': contact_email, 'form_content': form_content,})

            content = template.render(context)

            email = EmailMessage(
                "New contact form submission",
                content,
                "Your website" +'',
                ['steve@steve-shead.com'],
                headers = {'Reply-To': contact_email }
            )
            email.send()
            return render(request, 'contact/thank_you.html')

    return render(request, 'contact/contact.html', {
        'form': form_class,
    })