关键字不能是表达式-视图

时间:2019-05-21 06:46:05

标签: python django django-views

我正在尝试使用Django创建数据库并将其填充为较低的URL,但出现此错误:

File "C:\Final\vj_2\vj_2\app_1\views.py", line 12
    'naslov'=naslov,
    ^ SyntaxError: keyword can't be an expression

我不知道我在这里想念什么,为什么它不起作用。

这是我的观点:

def dodajClanak(request, naslov, datumObjave, autor):
    clan = Clanak(
        'naslov'=naslov,
        'datumObjave'=datumObjave,
        'autor'=autor
        )
    clan.save()#ako podaci nisu dobri throwat ce error
    return render(request, 'allapp.html', {'data': naslov + ' ' + datumObjave + ' ' + autor})

我的模特:

class Clanak(models.Model):
    naslov = models.CharField(null=False, blank=True, max_length=120)
    #naslov = models.DecimalField(null=False, blank=False, decimal_places=1, max_digits=5)
    datumObjave = models.DateField(null=False, blank=False)
    autor = models.CharField(null=False, blank=True, max_length=50)

    def __str__(self):
        return str(self.naslov) + ', ' + str(self.datumObjave) + ', ' + str(self.autor)

完整错误日志:

  

线程django-main-thread中的异常:追溯(最近一次调用)   最后):文件   “ c:\ users \%username%\ appdata \ local \ programs \ python \ python37-32 \ Lib \ threading.py”,   _bootstrap_inner中的第917行       self.run()文件“ c:\ users \%username%\ appdata \ local \ programs \ python \ python37-32 \ Lib \ threading.py”,   865行       self._target(* self._args,** self._kwargs)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ utils \ autoreload.py”,   包装中的第54行       fn(* args,** kwargs)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ core \ management \ commands \ runserver.py”,   第117行,在inner_run中       self.check(display_num_errors = True)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ core \ management \ base.py”,   390行,检查中       include_deployment_checks = include_deployment_checks,文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ core \ management \ base.py”,   _run_checks中的第377行       返回checks.run_checks(** kwargs)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ core \ checks \ registry.py”,   第72行,在run_checks中       new_errors =检查(app_configs = app_configs)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ core \ checks \ urls.py”,   第13行,在check_url_config中       返回check_resolver(resolver)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ core \ checks \ urls.py”,   第23行,在check_resolver中       返回check_method()文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ urls \ resolvers.py”,行   398,支票中       用于self.url_patterns中的模式:文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ utils \ functional.py”,   第80行,获取       res =实例。 dict [self.name] = self.func(instance)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ urls \ resolvers.py”,线   579,以url_patterns       模式= getattr(self.urlconf_module,“ urlpatterns”,self.urlconf_module)文件   “ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ utils \ functional.py”,   第80行,获取       res =实例。 dict [self.name] = self.func(instance)文件“ C:\ Final \ vj_2 \ venv \ lib \ site-packages \ django \ urls \ resolvers.py”,线   572,在urlconf_module中       返回import_module(self.urlconf_name)文件“ C:\ Final \ vj_2 \ venv \ lib \ importlib__init __。py”,行127,在   import_module       在_gcd_import文件中,返回_bootstrap._gcd_import(name [level:],程序包,级别)文件“”,第1006行   _find_and_load文件中的“”行983   “”,第967行,在_find_and_load_unlocked中   _load_unlocked中的文件“”,行677,
  文件“”,第728行,位于   exec_module文件“”,第219行,在   _call_with_frames_removed文件“ C:\ Final \ vj_2 \ vj_2 \ vj_2 \ urls.py”,第18行,在       从app_1导入视图文件“ C:\ Final \ vj_2 \ vj_2 \ app_1 \ views.py”,第12行       'naslov'= naslov,       ^ SyntaxError:关键字不能是表达式

urls.py

from django.contrib import admin
from django.urls import path, re_path
from app_1 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', views.home),
    re_path('add/(?P<naslov>[a-zA-Z]+/(?P<datumObjave>[0-9]+)/(?P<autor>[a-zA-Z]+', views.dodajClanak),
]

3 个答案:

答案 0 :(得分:1)

def dodajClanak(request, naslov, datumObjave, autor):
clan = Clanak(
    'naslov':naslov,
    'datumObjave':datumObjave,
    'autor':autor
    )
clan.save()#ako podaci nisu dobri throwat ce error
context ={
    data:"naslov + ' ' + datumObjave + ' ' + autor"
}
return render(request, 'allapp.html', context)

'datum_objave'==> datumObjave

答案 1 :(得分:1)

尝试以下代码:

# views.py
def dodajClanak(request, naslov, datumObjave, autor):
    Clanak.objects.create(
        naslov=naslov,
        datumObjave=datumObjave,
        autor=autor
    )
    return render(request, 'allapp.html', {'data': f'{naslov} {datumObjave} {autor}'})

# urls.py
from django.contrib import admin
from django.urls import path, re_path
from app_1 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', views.home),
    re_path('add/(?P<naslov>[a-zA-Z]+)/(?P<datumObjave>[-/0-9]+)/(?P<autor>[a-zA-Z]+)/', views.dodajClanak),
]

答案 2 :(得分:0)

def dodajClanak(request, naslov, datumObjave, autor):
    clan = Clanak(
        'naslov':naslov,
        'datum_objave':datumObjave,
        'autor':autor
        )
    clan.save()#ako podaci nisu dobri throwat ce error
    return render(request, 'allapp.html', {'data': naslov + ' ' + datumObjave + ' ' + autor})